Feedback

Chat Icon

Practical MCP with FastMCP & LangChain

Engineering the Agentic Experience

MCP Communication & Data Exchange Mechanisms
20%

The JSON-RPC Message Structure

A JSON-RPC message is a JSON object that can be one of the following types: request, response, or notification.

The Request Message

The request message is used to invoke a method on the server. In the context of MCP, this is how the client calls a tool or requests information from the server. It must include the following fields:

  • jsonrpc: The protocol version. This must always be "2.0".
  • id: A unique identifier for the request. It can be a string or a number and is used to match the response to the request.
  • method: The name of the method to invoke.
  • params: An object or array containing the parameters for the method.

Here is an example of a JSON-RPC request message:

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "sum",
    "arguments": { "a": 1, "b": 2 }
  },
  "id": 1
}

Without the id field, the message would be considered a notification, which means that the client does not expect a response from the server. The id in also what makes concurrent requests safe; the client can send multiple requests without waiting for the previous ones to complete, and the server can respond to them in any order. The client matches responses to requests using their identifiers.

The Response Message

A response is sent back by the server for every request that includes an id. In the context of MCP, this is how the server returns the result of a tool call or an error message. A response message must include the following fields:

  • jsonrpc: The protocol version. This must always be "2.0".
  • id: The same identifier as the request to which this is a response.
  • result: The result of the method call. This can be any JSON value.
  • error: An error object if the request itself could not be processed (for example, unknown method, malformed request, or server failure). This must include a code (a number indicating the error type) and a message (a string describing the error). It can also include a data field with additional information about the error.

Here is an example of a JSON-RPC response message for a successful request:

{
  "jsonrpc": "2.0",
  "id": 1, // <-- matches the request id
  "result": {
    "content": [
      { 
        "type": "text", 
        "text": "The sum is 3." 
      }
    ],
    "isError": false
  }
}

It is important to understand the difference between two kinds of errors in MCP:

Tool execution errors happen when the tool itself runs but encounters a problem (for example, invalid input values, an API failure, or a business logic error). These are returned as a normal result with isError set to true. This allows the AI model to see the error and potentially self-correct.

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Error: Invalid parameters. 'a' and 'b' must be numbers."
      }
    ],
    "isError": true

Practical MCP with FastMCP & LangChain

Engineering the Agentic Experience

Enroll now to unlock current content and receive all future updates for free. Your purchase supports the author and fuels the creation of more exciting content. Act fast, as the price will rise as the course nears completion!