monitoringHowTo

Run Splunk Searches from an MCP Server

Execute SPL searches against Splunk's REST API as an MCP tool, handling the asynchronous create-poll-retrieve job lifecycle.

Quick Answer / TL;DR

Splunk searches run as asynchronous jobs through the REST API: create the job, poll its status until it reports done, then fetch results - a single blocking HTTP call is not enough on its own.

Key Takeaways

  • Splunk search jobs are asynchronous by default; exec_mode=blocking simplifies this to one call but still has its own timeout to manage.
  • SPL pipes commands together with |, starting from an index filter and narrowing down - always scope to a specific index rather than searching all indexes.
  • Always pass an explicit earliest_time - an unscoped search over all time on a large index is expensive and slow.

Creating and reading a search job

With exec_mode set to blocking, the create call itself waits for the search to finish (up to its own timeout), which avoids a manual poll loop for most queries short enough to run inline.

typescript
async function splunkSearch(query: string, earliestTime = "-1h", maxResults = 100) {
  const base = process.env.SPLUNK_URL!; // e.g. https://splunk.example.com:8089
  const auth = { Authorization: `Bearer ${process.env.SPLUNK_TOKEN}` };

  const createRes = await fetch(`${base}/services/search/jobs?output_mode=json`, {
    method: "POST",
    headers: { ...auth, "Content-Type": "application/x-www-form-urlencoded" },
    body: new URLSearchParams({
      search: `search ${query}`,
      earliest_time: earliestTime,
      latest_time: "now",
      max_count: String(maxResults),
      exec_mode: "blocking",
    }),
  });
  const { sid } = await createRes.json();

  const resultsRes = await fetch(
    `${base}/services/search/jobs/${sid}/results?output_mode=json`,
    { headers: auth },
  );
  const { results } = await resultsRes.json();
  return results;
}

// Example: SPL scoped to one index with a time bound
await splunkSearch('index=web status=5* | stats count by uri_path', "-1h");

Run Splunk Searches from an MCP Server FAQs

Direct answers for developers, operators, and Indian teams evaluating MCP.

M
MCPserver Team

MCP documentation and protocol implementation team

Published: 2026-07-21
Updated: 2026-07-21

References & Technical Specifications