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

Get the Last JSON-RPC Response from an MCP Server

See more MCP Client Examples

Demonstrates Mcp.GetLastJsonData, which returns the complete JSON-RPC response envelope (the jsonrpc, id, and result members) from the most recent request into a JsonObject.

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

Background. This exposes the full protocol-level response for inspection or debugging after any MCP request.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMcp
    Variant vToolsResult
    Handle hoToolsResult
    Variant vJson
    Handle hoJson
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatMcp)) To hoMcp
    If (Not(IsComObjectCreated(hoMcp))) Begin
        Send CreateComObject of hoMcp
    End

    //  Connect to a public MCP (Model Context Protocol) server over its Streamable HTTP endpoint.  The
    //  Microsoft Learn MCP server is public and requires no authentication.
    Get ComConnect Of hoMcp "https://learn.microsoft.com/api/mcp" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMcp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Perform an MCP request.
    Get Create (RefClass(cComChilkatJsonObject)) To hoToolsResult
    If (Not(IsComObjectCreated(hoToolsResult))) Begin
        Send CreateComObject of hoToolsResult
    End
    Get pvComObject of hoToolsResult to vToolsResult
    Get ComListTools Of hoMcp vToolsResult To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMcp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the complete JSON-RPC response envelope (the jsonrpc, id, and result members) from the most
    //  recent request.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get pvComObject of hoJson to vJson
    Send ComGetLastJsonData To hoMcp vJson

    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1
    //  JSON parsing code for this result can be generated at Chilkat's online tool:
    //  https://tools.chilkat.io/jsonParse


End_Procedure