Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vMcp
    Handle hoMcp
    Variant vToolsJson
    Handle hoToolsJson
    Handle hoAi
    Variant vSbResponse
    Handle hoSbResponse
    Variant vConvoJson
    Handle hoConvoJson
    String sTemp1
    String sTemp2

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatMcp)) To hoMcp
    If (Not(IsComObjectCreated(hoMcp))) Begin
        Send CreateComObject of hoMcp
    End
    //  mcp.AuthToken = "...";   // <-- only if the server requires a bearer token
    Get ComConnect Of hoMcp "https://mcp.deepwiki.com/mcp" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMcp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComServerName Of hoMcp To sTemp1
    Get ComServerVersion Of hoMcp To sTemp2
    Showln "Connected to MCP server: " sTemp1 " (version " sTemp2 ")"

    //  (Optional) Show the tools the server advertises.  This is the same set of tools Chilkat registers
    //  with the model in the UseMcp call below.
    Get Create (RefClass(cComChilkatJsonObject)) To hoToolsJson
    If (Not(IsComObjectCreated(hoToolsJson))) Begin
        Send CreateComObject of hoToolsJson
    End
    Set ComEmitCompact Of hoToolsJson To False
    Get pvComObject of hoToolsJson to vToolsJson
    Get ComListTools Of hoMcp vToolsJson To iSuccess
    If (iSuccess = True) Begin
        Showln "Available MCP tools:"
        Get ComEmit Of hoToolsJson To sTemp1
        Showln sTemp1
        Showln "----"
    End

    //  2) Set up the AI conversation and register the MCP tools.
    Get Create (RefClass(cComChilkatAi)) To hoAi
    If (Not(IsComObjectCreated(hoAi))) Begin
        Send CreateComObject of hoAi
    End
    Set ComProvider Of hoAi To "anthropic"
    //  The API key should come from a secure source rather than being hard-coded.
    Set ComApiKey Of hoAi To "AI_PROVIDER_API_KEY"
    Set ComModel Of hoAi To "claude-sonnet-5"
    Set ComVerboseLogging Of hoAi To True

    //  Automatic tool use (MCP or JavaScript) requires a conversation -- it cannot be used with a
    //  stateless query, so create one with NewConvo.
    Get ComNewConvo Of hoAi "test_conversation" "You are a helpful assistant.  Use the available tools when they can help answer the question." "" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get pvComObject of hoMcp to vMcp
    Get ComUseMcp Of hoAi vMcp "deepwiki" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get ComInputAddText Of hoAi '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.' To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get ComAsk Of hoAi "text" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The final assistant answer (after any automatic tool calls).
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End
    Get pvComObject of hoSbResponse to vSbResponse
    Get ComGetOutputTextSb Of hoAi vSbResponse To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Assistant answer:"
    Get ComGetAsString Of hoSbResponse To sTemp1
    Showln sTemp1
    Showln "----"

    //  (Optional) Dump the full transcript, which now includes the tool call and the tool result that
    //  were added automatically.
    Get Create (RefClass(cComChilkatJsonObject)) To hoConvoJson
    If (Not(IsComObjectCreated(hoConvoJson))) Begin
        Send CreateComObject of hoConvoJson
    End
    Set ComEmitCompact Of hoConvoJson To False
    Get pvComObject of hoConvoJson to vConvoJson
    Get ComExportConvo Of hoAi "test_conversation" vConvoJson To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Full Conversation:"
    Get ComEmit Of hoConvoJson To sTemp1
    Showln sTemp1

    //  4) Close the MCP session (best effort; releases the server-side session).
    Get ComClose Of hoMcp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMcp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure