MCP Tools
This example connects a remote MCP tool server, discovers tools from it, and runs those tools inside the normal smart runtime.
Use this when
- tool capabilities live outside your process
- you want remote tools to participate in the same planning and tracing loop as local tools
- you need a real MCP integration reference instead of a conceptual guide
What it shows
MultiServerMCPClientsetup- remote tool discovery
fromLangchainTools(...)bridge into Agent SDK- smart runtime planning over MCP tools
Run it
bash
cd examples
npm run example:mcp-tavilyCore code
ts
const client = new MultiServerMCPClient({
throwOnLoadError: true,
prefixToolNameWithServerName: true,
useStandardContentBlocks: true,
mcpServers: {
"tavily-remote-mcp": {
transport: "stdio",
command: "npx",
args: ["-y", "mcp-remote", `https://mcp.tavily.com/mcp/?tavilyApiKey=${TAVILY_API_KEY}`],
env: {},
},
},
});
const tools = fromLangchainTools(await client.getTools());
const agent = createSmartAgent({
model,
tools,
useTodoList: true,
limits: { maxToolCalls: 10 },
summarization: { enable: true, maxTokens: 6000 },
});Environment notes
This example expects the relevant provider credentials, including the Tavily API key and the model provider key used by the script.
End-to-end flow
- An MCP client is created with a Tavily-backed server definition.
- The client discovers remote tools.
fromLangchainTools(...)adapts them into SDK-native tools.- A smart agent uses those tools with planning and summarization enabled.
- The client is closed in a
finallyblock after the run.
Why this example matters
It proves that MCP tools do not need a separate orchestration path. They can live inside the same planning, tracing, and summarization loop as local tools.
What to inspect
- the MCP client bootstrap
- tool discovery through
client.getTools() - conversion with
fromLangchainTools(...) - cleanup with
client.close()in thefinallyblock
Production takeaway
This is the reference pattern when MCP is part of the production tool surface rather than a separate experimental path.
Expected output
- discovered MCP tool names are printed before the run
- if credentials are present, the agent returns a short Tavily-backed summary
- if keys are missing, the script explains why the agent run is skipped
Common failure modes
OPENAI_API_KEYorTAVILY_API_KEYis missing- the remote MCP transport fails to connect or load tools
- the example is interrupted before
client.close()executes, leaving cleanup incomplete