Client API
The Client struct is used to connect to MCP servers and send MCP requests.
Constructor
Client.init
pub fn init(config: ClientConfig) ClientCreate a new MCP client.
Parameters:
| Field | Type | Description |
|---|---|---|
name | []const u8 | Client name (required) |
version | []const u8 | Client version (required) |
Example:
var client: mcp.Client = .init(io, allocator, .{
.name = "my-client",
.version = "1.0.0",
});
defer client.deinit(allocator);Lifecycle
Client.deinit
pub fn deinit(self: *Client, allocator: std.mem.Allocator) voidClean up client resources and pending state.
Capabilities
Client.enableRoots
pub fn enableRoots(self: *Client, listChanged: bool) voidEnable the roots capability. Allows the client to provide filesystem roots to the server. listChanged indicates whether the client will send notifications when roots change.
Client.enableSampling
pub fn enableSampling(self: *Client) voidEnable the sampling capability. Allows the server to request LLM completions.
Client.enableSamplingAdvanced
pub fn enableSamplingAdvanced(self: *Client, context: bool, tools_support: bool) voidEnable the sampling capability with advanced configuration (context inclusion and tool use).
Client.enableElicitation
pub fn enableElicitation(self: *Client) voidEnable the elicitation capability (both form and URL modes) for handling server-initiated user input requests.
Client.enableElicitationForm
pub fn enableElicitationForm(self: *Client) voidClient.enableElicitationUrl
pub fn enableElicitationUrl(self: *Client) voidClient.enableTasks
pub fn enableTasks(self: *Client) voidEnable the tasks capability for managing long-running operations.
Roots Management
Client.addRoot
pub fn addRoot(self: *Client, allocator: std.mem.Allocator, uri: []const u8, name: ?[]const u8) !voidAdd a filesystem root.
Parameters:
uri- URI of the root (e.g.,file:///home/user/project)name- Human-readable name for the root
Example:
try client.addRoot(allocator, "file:///home/user/documents", "Documents");
try client.addRoot(allocator, "file:///home/user/projects", "Projects");Fields
client.config
pub const config: ClientConfigThe client configuration.
client.allocator
pub const allocator: AllocatorThe memory allocator.
client.roots_list
pub const roots_list: ArrayList(types.Root)List of configured roots.
client.capabilities
pub const capabilities: ClientCapabilitiesEnabled capabilities.
Connection Management
Client.connectStdio
pub fn connectStdio(self: *Client, io: std.Io, allocator: std.mem.Allocator, command: []const u8, args: []const []const u8) !voidClient.connectHttp
pub fn connectHttp(self: *Client, io: std.Io, allocator: std.mem.Allocator, url: []const u8) !voidClient.setAuthorizationToken
pub fn setAuthorizationToken(self: *Client, token: []const u8) !voidSet bearer token before calling connectHttp when your HTTP server requires authorization.
Client.disconnect
pub fn disconnect(self: *Client) voidRequest APIs
All request APIs currently send protocol requests and return !void.
Tools
pub fn listTools(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void
pub fn callTool(self: *Client, io: std.Io, allocator: std.mem.Allocator, name: []const u8, arguments: ?std.json.Value) !voidResources
pub fn listResources(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void
pub fn readResource(self: *Client, io: std.Io, allocator: std.mem.Allocator, uri: []const u8) !void
pub fn subscribeResource(self: *Client, io: std.Io, allocator: std.mem.Allocator, uri: []const u8) !void
pub fn unsubscribeResource(self: *Client, io: std.Io, allocator: std.mem.Allocator, uri: []const u8) !void
pub fn listResourceTemplates(self: *Client, io: std.Io, allocator: std.mem.Allocator) !voidPrompts
pub fn listPrompts(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void
pub fn getPrompt(self: *Client, io: std.Io, allocator: std.mem.Allocator, name: []const u8, arguments: ?std.json.Value) !voidCompletion / Logging / Health
pub fn complete(self: *Client, io: std.Io, allocator: std.mem.Allocator, ref: std.json.Value, argument: std.json.Value) !void
pub fn setLogLevel(self: *Client, io: std.Io, allocator: std.mem.Allocator, level: []const u8) !void
pub fn ping(self: *Client, io: std.Io, allocator: std.mem.Allocator) !voidTasks
pub fn getTask(self: *Client, io: std.Io, allocator: std.mem.Allocator, taskId: []const u8) !void
pub fn getTaskResult(self: *Client, io: std.Io, allocator: std.mem.Allocator, taskId: []const u8) !void
pub fn listTasks(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void
pub fn cancelTask(self: *Client, io: std.Io, allocator: std.mem.Allocator, taskId: []const u8) !voidNotifications
pub fn notifyInitialized(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void
pub fn notifyRootsChanged(self: *Client, io: std.Io, allocator: std.mem.Allocator) !voidComplete Example
const std = @import("std");
const mcp = @import("mcp");
pub fn main(init: std.process.Init) void {
run(init.io, init.gpa) catch |err| mcp.reportError(err);
}
fn run(io: std.Io, allocator: std.mem.Allocator) !void {
// Create client
var client: mcp.Client = .init(io, allocator, .{
.name = "full-client",
.version = "1.0.0",
});
defer client.deinit(allocator);
// Enable capabilities
client.enableRoots(true);
client.enableSampling();
// Configure roots
try client.addRoot(allocator, "file:///home/user/docs", "Documentation");
try client.addRoot(allocator, "file:///home/user/code", "Source Code");
// Print configuration
std.debug.print("Client: {s} v{s}\n", .{
client.config.name,
client.config.version,
});
std.debug.print("Roots: {d}\n", .{client.roots_list.items.len});
// In a full implementation, you would:
// 1. Connect to a server via transport
// 2. Send initialize request
// 3. Interact with server capabilities
}Connection Management
Client.connectStdio
pub fn connectStdio(self: *Client, io: std.Io, allocator: std.mem.Allocator, command: []const u8, args: []const []const u8) !voidConnect to a server using the STDIO transport. Note: Full sub-process spawning may require platform-specific tuning in real usage. Currently stubbed in simple scenarios.
Client.connectHttp
pub fn connectHttp(self: *Client, io: std.Io, allocator: std.mem.Allocator, url: []const u8) !voidConnect to a server via HTTP transport.
Client.setAuthorizationToken
pub fn setAuthorizationToken(self: *Client, token: []const u8) !voidSets the authorization token for Bearer auth (OAuth 2.1 support) before making HTTP connections. Call this before connectHttp.
Client.disconnect
pub fn disconnect(self: *Client) voidDisconnects from the server and closes the active transport.
Server Queries and Interactions
Tools
/// Request the list of available tools
pub fn listTools(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void
// Request operations are currently fire-and-handle-later style
try client.listTools(io, allocator);
try client.callTool(io, allocator, "hello", null);
/// Request the list of available resources
pub fn listResources(self: *Client, io: std.Io, allocator: std.mem.Allocator) !void