Skip to main content

Install SEC API in LlamaIndex

Use LlamaIndex’s MCP tooling to register SEC API as a streamable HTTP server. The example passes auth through a custom httpx.AsyncClient.

Prerequisites

  • SEC API key. Sign up at secapi.ai. Set SECAPI_API_KEY.
  • llama-index-tools-mcp, llama-index-core, llama-index-llms-anthropic, and httpx.

Install

pip install llama-index-tools-mcp llama-index-core llama-index-llms-anthropic httpx
import asyncio, os
import httpx
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.anthropic import Anthropic

async def main():
    http = httpx.AsyncClient(headers={"x-api-key": os.environ["SECAPI_API_KEY"]})
    mcp = BasicMCPClient("https://api.secapi.ai/mcp", http_client=http)
    tools = await McpToolSpec(client=mcp).to_tool_list_async()

    agent = FunctionAgent(tools=tools, llm=Anthropic(model="claude-sonnet-4-5"))
    response = await agent.run("Show me Apple's latest 8-K filing.")
    print(response)

asyncio.run(main())

Verify

Run the snippet. The agent should call the filings.latest tool with ticker: AAPL, form: 8-K and print the filing URL plus a short summary.

Troubleshooting

  • 401 Unauthorized — the httpx.AsyncClient headers didn’t reach the server. Verify the env var is set in the same shell that runs Python: echo $SECAPI_API_KEY.
  • 429 Too Many Requests — rate limit hit. See GET https://api.secapi.ai/v1/billing for your plan’s cap.
  • AttributeError: BasicMCPClient has no attribute headers — if your installed version does not accept headers=, wrap auth in a custom httpx.AsyncClient as shown above.
  • Connection timeout — verify the URL ends in /mcp (note the /mcp suffix, not /sse).

OAuth alternative

For longer-lived workflows that need rotating credentials, swap BasicMCPClient(...) for BasicMCPClient.with_oauth(...) and follow the OAuth-protected-resource metadata at https://api.secapi.ai/.well-known/oauth-protected-resource.

Next