MCPClient
A connection to an MCP server, and the tools it exposes.
Connects to a Model Context Protocol server over stdio, streamable HTTP, or the legacy SSE transport, and turns the tools it advertises into tools your model can call.
The tools it returns are ordinary Tool values, so
they mix freely with ones you wrote yourself. Servers that require OAuth are
handled by attaching a session; the client refreshes and retries on a 401
rather than making you catch it.
let client = try await MCPClient(
transport: .streamableHTTP(url: URL(string: "https://mcp.example.com")!)
)
let result = try await generateText(
model: AnthropicModel("claude-sonnet-5"),
prompt: prompt,
tools: try await client.tools()
)Declaration
public actor MCPClientDefined in Sources/AI/MCP/MCPClient.swift.
Initializer
init(
transport: any MCPTransport
)Properties
| Property | Type | Default |
|---|---|---|
let transport | any MCPTransport | — |
Methods
func close() asyncfunc connect(
clientName: String = "swift-ai-sdk",
clientVersion: String = "0.3.0"
) async throwsfunc tools() async throws -> [any AIToolProtocol]func callTool(
name: String,
arguments: JSONValue
) async throws -> JSONValue