Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.6.0+

Non-Streaming End-to-End MCP Tool Use with an AI Model

See more AI Examples

Demonstrates Ai.UseMcp in a complete, non-streaming Model Context Protocol (MCP) workflow. The example connects to an MCP server, registers the server's tools with an AI model via UseMcp, and asks a question the model can only answer well by calling one of those tools. When the model requests a tool, Chilkat automatically calls it on the MCP server, feeds the result back into the conversation, and continues — all inside a single (non-streaming) Ask.

Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.

Background. Automatic tool use requires a conversation (created with NewConvo); it cannot be used with a stateless query. Each registered tool name is namespaced with a prefix (here deepwiki) so tools from different servers cannot collide. The Mcp object must stay connected and in scope for as long as its tools are in use during the conversation. This example uses DeepWiki, a free, public, no-authentication Streamable HTTP MCP server; swap in any MCP server URL, setting AuthToken before Connect if a bearer token is required.
Using multiple MCP servers. More than one MCP server can be used in the same conversation. Connect each server and call UseMcp once per server — one call per MCP server — giving each a distinct prefix (for example deepwiki, weather, github) so tools from different servers never collide.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Non-streaming, end-to-end MCP example.  It connects to an MCP server, hands the server's tools to
    // an AI model via Ai.UseMcp, and asks a question that the model can only answer by calling one of
    // those tools.  When the model requests a tool, Chilkat automatically calls it on the MCP server,
    // feeds the result back into the conversation, and continues -- all inside a single (non-streaming)
    // Ask.
    //
    // The MCP server used here is DeepWiki: a free, public, no-authentication Streamable HTTP server
    // that answers questions about public GitHub repositories.  Swap in any MCP server URL; if it
    // requires a bearer token, set mcp.AuthToken before Connect.

    // 1) Connect to the MCP server.
    const mcp = try chilkat.Mcp.init();
    defer mcp.deinit();
    // mcp.AuthToken = "..."; // <-- only if the server requires a bearer token
    mcp.connect("https://mcp.deepwiki.com/mcp") catch {
        std.debug.print("{s}\n", .{try mcp.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Connected to MCP server: {s} (version {s})\n", .{ try mcp.getServerName(alloc), try mcp.getServerVersion(alloc) });

    // (Optional) Show the tools the server advertises.  This is the same set of tools Chilkat registers
    // with the model in the UseMcp call below.
    const tools_json = try chilkat.JsonObject.init();
    defer tools_json.deinit();
    tools_json.setEmitCompact(false);
    if (mcp.listTools(tools_json)) {
        std.debug.print("Available MCP tools:\n", .{});
        std.debug.print("{s}\n", .{try tools_json.emit(alloc)});
        std.debug.print("----\n", .{});
    } else |_| {}

    // 2) Set up the AI conversation and register the MCP tools.
    const ai = try chilkat.Ai.init();
    defer ai.deinit();
    ai.setProvider("anthropic");
    // The API key should come from a secure source rather than being hard-coded.
    ai.setApiKey("AI_PROVIDER_API_KEY");
    ai.setModel("claude-sonnet-5");
    ai.setVerboseLogging(true);

    // Automatic tool use (MCP or JavaScript) requires a conversation -- it cannot be used with a
    // stateless query, so create one with NewConvo.
    ai.newConvo("test_conversation", "You are a helpful assistant.  Use the available tools when they can help answer the question.", "") catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    // Register the connected server's tools with the model.  Each tool name is namespaced with the
    // "deepwiki" prefix so that tools from different servers (and JavaScript tools) can never collide --
    // e.g. "deepwiki_ask_question".  The Mcp object must stay connected and in scope for as long as its
    // tools are in use during the conversation.
    ai.useMcp(mcp, "deepwiki") catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    // Multiple MCP servers can be used in the same conversation.  Connect each server and call
    // UseMcp once per server, giving each a distinct prefix so their tool names cannot collide.

    // A prompt the model can only answer well by calling one of the MCP tools.
    ai.inputAddText("Use the available tools to look up the GitHub repository \"modelcontextprotocol/modelcontextprotocol\", then give me a brief, sourced summary of which transports the Model Context Protocol defines.") catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    // 3) Non-streaming Ask.  Any MCP tool the model requests is executed automatically inside this call
    // (Chilkat -> Mcp.CallTool -> result back into the transcript -> continue), so Ask returns only once
    // the model produces its final answer.
    ai.ask("text") catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    // The final assistant answer (after any automatic tool calls).
    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    ai.getOutputTextSb(sb_response) catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Assistant answer:\n", .{});
    std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
    std.debug.print("----\n", .{});

    // (Optional) Dump the full transcript, which now includes the tool call and the tool result that
    // were added automatically.
    const convo_json = try chilkat.JsonObject.init();
    defer convo_json.deinit();
    convo_json.setEmitCompact(false);
    ai.exportConvo("test_conversation", convo_json) catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Full Conversation:\n", .{});
    std.debug.print("{s}\n", .{try convo_json.emit(alloc)});

    // 4) Close the MCP session (best effort; releases the server-side session).
    mcp.close() catch {
        std.debug.print("{s}\n", .{try mcp.getLastErrorText(alloc)});
        return;
    };
}