addMcpServer

View as MarkdownOpen in Claude

Connect the agent to an external Model Context Protocol (MCP) server. Tools are discovered via the MCP protocol at session start and automatically registered as SWAIG functions. Optionally, the server’s resources can be fetched into the agent’s global data.

This method connects your agent to an MCP server as a client. To expose your agent’s own tools as an MCP server, use enableMcpServer().

Parameters

url
stringRequired

MCP server HTTP endpoint URL.

opts
object

Optional configuration.

opts.headers
Record<string, string>

HTTP headers sent with every request to the MCP server (e.g., { Authorization: 'Bearer sk-xxx' }).

opts.resources
boolean

Whether to fetch the server’s resources into the agent’s global_data.

opts.resourceVars
Record<string, string>

Variables for URI template substitution when fetching resources. Supports SignalWire call variables such as ${caller_id_number}.

Returns

AgentBase — Returns this for method chaining.

Example

1import { AgentBase } from '@signalwire/sdk';
2
3const agent = new AgentBase({ name: 'assistant', route: '/assistant' });
4agent.setPromptText('You are a helpful assistant.');
5agent.addMcpServer('https://mcp.example.com/sse', {
6 headers: { Authorization: 'Bearer sk-xxx' },
7 resources: true,
8 resourceVars: { caller_id: '${caller_id_number}' },
9});
10await agent.serve();