> ## Documentation Index
> Fetch the complete documentation index at: https://docs.secapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect SEC API to LangChain

> Discover SEC API MCP tools in a server-side LangChain application, verify one filing call, and preserve its source record.

Use LangChain's MCP adapters when your application, worker, or backend owns the
agent. The adapter loads SEC API's remote tool definitions over streamable HTTP
and returns LangChain tools. Put the API key in that server runtime's secret
manager; browser bundles and client-side routes should not receive it.

## Make one direct tool call first

Install the adapter and your chosen model integration:

```bash theme={null}
pip install langchain-mcp-adapters langchain
```

Then verify tool discovery and a single filing call before constructing an
agent:

```python theme={null}
import asyncio
import os

from langchain_mcp_adapters.client import MultiServerMCPClient


async def main() -> None:
    client = MultiServerMCPClient(
        {
            "secapi": {
                "transport": "http",
                "url": "https://api.secapi.ai/mcp",
                "headers": {"x-api-key": os.environ["SECAPI_API_KEY"]},
            }
        }
    )
    tools = await client.get_tools()
    latest_filing = next(tool for tool in tools if tool.name == "filings.latest")
    result = await latest_filing.ainvoke({"ticker": "AAPL", "form": "10-K"})
    print(result)


asyncio.run(main())
```

LangChain documents `http` as the streamable HTTP transport and supports custom
headers in the connection configuration. Its [MCP adapter documentation](https://docs.langchain.com/oss/python/langchain/mcp)
is the version-specific source for package and transport changes. Load
`SECAPI_API_KEY` only in the process that invokes this code. For a web
application, call this from a server route, worker, or backend service; do not
return the key or a reusable MCP client configuration to the browser.

## Add tools only after the proof call works

After the direct call succeeds, pass the discovered `tools` to your LangChain
agent. Keep the tool loading near the agent lifecycle: rebuild or refresh it on
process startup and after a deliberate integration upgrade, rather than
serializing an old copied schema into your application. Give the agent narrow
defaults for issuer, form, date range, and list limits before allowing it to
plan multi-tool work.

## Return the source record with the answer

`filings.latest` returns a filing record with `accessionNumber`, `filingDate`,
and `filingUrl`; the MCP response envelope carries `requestId` and
`traceparent`. Store those with the answer. Search, section, and analytical
tools may include provenance, freshness, or source fields: carry them through
your agent state and final output. A model's prose is not a substitute for the
source filing identity.

## Errors and bounded retries

Handle tool errors before model-level retry loops:

* `401`: repair or rotate the server-side key.
* `402`: resolve plan, credit, billing, or budget state; do not retry unchanged.
* `429` and temporary `503`: honor `Retry-After`, reduce concurrency, and retry
  a bounded number of times.
* `mcp_tool_timeout`: reduce filters or result limits before retrying.

Record the JSON-RPC error, SEC API `error.data.code`, and request ID. Read the
[Error code catalog](/error-code-catalog) before turning retries on globally.

## Next step

Add the verified tool set to your LangChain agent, then require every filing
answer to include accession number, filing URL, and request metadata.
