Agents and MCP

LangChain

Load Tracehold as MCP tools in a LangChain agent.

Install#

pip install langchain langchain-mcp-adapters langchain-anthropic

Wire it up#

import os
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropic

async def main():
    client = MultiServerMCPClient({
        "tracehold": {
            "transport": "streamable_http",
            "url": os.environ["TRACEHOLD_MCP_URL"],
            "headers": {
                "Authorization": f"Bearer {os.environ['TRACEHOLD_MCP_TOKEN']}",
            },
        },
    })
    tools = await client.get_tools()

    agent = create_agent(
        ChatAnthropic(model="claude-sonnet-5-5"),
        tools,
        system_prompt="If Tracehold holds a call for approval, keep calling tracehold_wait_for_approval with its Action ID until it is decided.",
    )

    result = await agent.ainvoke({
        "messages": [{"role": "user", "content": "List my S3 buckets"}],
    })
    print(result["messages"][-1].content)

if __name__ == "__main__":
    asyncio.run(main())

Run#

export TRACEHOLD_MCP_URL=https://api.tracehold.ai/mcp/<server-id>/   # the server's endpoint in Tracehold
export TRACEHOLD_MCP_TOKEN=<your-mcp-token>
export ANTHROPIC_API_KEY=<your-anthropic-key>
python agent.py

Verify#

Tool calls flow through the Tracehold gateway; each one appears in Tracehold under Activity with the decision and why.

When a call waits for a person#

tracehold_wait_for_approval is among the tools get_tools() returns, and the system prompt above has the agent wait with it until a person decides. A held call comes back to the model as a tool message with status error and the run goes on, so the model reads it and waits; tested with langchain 1.4.4 and langchain-mcp-adapters 0.3.2. (create_react_agent from langgraph.prebuilt works the same way but is deprecated.) In a LangGraph workflow that should not keep running while it waits, stop at the held call (an interrupt), keep its Action ID, and resume when Tracehold's webhook arrives. get_tools() reads the server's tools when you call it: call it again for each run so added and removed tools are seen. See Agents in pipelines.