> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Messages and Schemas

> Explains using JSON schemas for tools and resources in a model-host-server protocol to prevent runtime errors and ensure correct tool calls

Alan types a question into his chat window. The language model decides it needs to call a shipping tool to answer. The model selects a tool using three pieces of information the host provides for each tool: its `name`, its `description`, and its `inputSchema`. If the `inputSchema` doesn't constrain arguments precisely, the model may guess and send the wrong type — causing runtime failures.

Example: the model built and sent this request:

```json theme={null}
{
  "name": "get_shipment",
  "arguments": {
    "shipment": 4417
  }
}
```

But Alan's tool expects a shipment reference like `PL-4417` (a string). Because the `inputSchema` did not require a string, the model sent a number (`4417`) instead of a string. The server returned a 500 error and the user saw a failure.

The missing schema constraint was a single line:

```json theme={null}
"shipment": {
  "type": "string"
}
```

With that line added to the tool's `inputSchema`, the model sent `"PL-4417"` and the call succeeded.

Before fixing the schema, let's review the three kinds of messages used by this protocol.

Message types

* Request: includes an `id` and expects a response.
* Response: contains the same `id` and either a `result` or an `error` (never both).
* Notification: has no `id` and therefore expects no reply.

Table: Message types at a glance

| Message Type | Identifying Field | Expects Reply | Example |
| - | - | - | - |
| Request | `id` | Yes | `{"id": 7, "method": "tools/call", "params": { /* ... */ }}` |
| Response | `id`, `result` / `error` | No (it's a reply) | `{"id": 7, "result": { /* ... */ }}` |
| Notification | no `id` | No | `{"method": "tools/event", "params": { /* ... */ }}` |

Request example:

```json theme={null}
{
  "id": 7,
  "method": "tools/call",
  "params": { /* ... */ }
}
```

Response examples:

```json theme={null}
{
  "id": 7,
  "result": { /* ... */ }
}
```

or

```json theme={null}
{
  "id": 7,
  "error": { /* ... */ }
}
```

Notification example (no `id`):

```json theme={null}
{
  "method": "tools/event",
  "params": { /* ... */ }
}
```

Client sends requests; the server sends responses and notifications. In this specification, servers do not initiate requests to clients — requests flow one-way (client → server).

Servers must not assume they can initiate requests to clients. Requests are one-way (client → server) in the current protocol; servers should only send responses or notifications back.

Every tool exposed by the server must include an `inputSchema` written using JSON Schema. A complete `inputSchema` does the following:

* Names each argument.
* Declares each argument's type (`string`, `number`, `object`, etc.).
* Marks which arguments are `required`.

The `inputSchema` is required for every tool. If an argument is unconstrained (e.g., `any`), the model may choose the wrong type and cause runtime errors on the server.

Correct `inputSchema` for `get_shipment`:

```json theme={null}
{
  "name": "get_shipment",
  "inputSchema": {
    "type": "object",
    "properties": {
      "shipment": { "type": "string" }
    },
    "required": ["shipment"]
  }
}
```

There is also an optional `outputSchema` that describes the structure of the response the tool returns. Providing an `outputSchema` is not mandatory, but it makes it much easier for host and client code to consume structured results.

Example `outputSchema` (optional):

```json theme={null}
{
  "outputSchema": {
    "type": "object",
    "properties": {
      "id": { "type": "string" },
      "status": { "type": "string" },
      "city": { "type": "string" },
      "days_late": { "type": "integer" }
    },
    "required": ["id", "status"]
  }
}
```

Why the schema matters (three critical places)

1. The model reads the `inputSchema` to decide which tool to call and to construct the arguments.
2. The client validates arguments against the `inputSchema` before sending the request.
3. The server validates arguments against the `inputSchema` on arrival and should reject requests that violate the schema, even if they originated from a trusted client.

Always validate on the server. Client-side validation can be bypassed; the server must enforce the schema to prevent runtime failures and protect backend logic.

After Alan's server was updated with the correct `inputSchema`, the model requested the tool again and sent the shipment as a string (`"PL-4417"`). The call succeeded and the server returned both a structured result and a human-readable text summary.

Human-readable text (for display):

```json theme={null}
{
  "type": "text",
  "text": "In Rotterdam, 2 days late"
}
```

Structured machine-readable result (matches `outputSchema`):

```json theme={null}
{
  "id": "PL-4417",
  "status": "in_transit",
  "city": "Rotterdam",
  "days_late": 2
}
```

Structured results help downstream code consume responses reliably, while the text summary is useful for user-facing chat.

Resources (files/documents) are handled differently from tools. You don't "call" a resource like a function; you look it up by its address (URI), similar to opening a URL in a browser. Every resource includes:

* `uri` (its identity)
* `name`
* optional `title` and `description`
* `mimeType` (e.g., `text/plain`, `application/pdf`)
* optional `size`
* contents as either `text` (for text data) or a base64-encoded `blob` (for binary)

Examples of resource metadata and content:

```json theme={null}
{
  "uri": "file:///PL-4417.pdf",
  "name": "PL-4417.pdf",
  "title": "Shipment manifest",
  "description": "Manifest for PL-4417",
  "mimeType": "application/pdf",
  "size": 18240
}
```

Text resource example:

```json theme={null}
{
  "mimeType": "text/plain",
  "text": "Pallets: 12"
}
```

Binary resource example (contents carried as base64-encoded string in `blob`):

```json theme={null}
{
  "mimeType": "application/pdf",
  "blob": "<base64-encoded-bytes>"
}
```

Now the whole flow:

1. The host discovers a server and lists the tools and resources it offers.
2. The host assembles the tool names, descriptions, and schemas into the model context.
3. The user asks a question.
4. The model decides a tool is needed and selects one based on the provided schemas and descriptions.
5. The host requests user approval (if applicable).
6. Once approved, the host sends a `tools/call` request to the server.
7. The server executes the tool and returns a result.
8. The host feeds the result back to the model, which produces the final user-facing answer.

![An infographic titled "The Whole Flow" showing a model (chip) on the left, a host/chat interface in the center (with the query "Where is PL-4417?"), and an MCP server on the right. Below the diagram are numbered workflow steps such as tools/list, Tool list, Context, Question, Pick a tool, Approve, tools/call, Execute, and Result.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Messages-Tools-and-Sessions/Messages-and-Schemas/whole-flow-model-chat-mcp.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=d155429889f10465c1d4973b3d8b3c93)

The model never talks to the server directly. It only sees the `name`, `description`, and `inputSchema` the host provides, and then it emits a request describing the tool name and arguments to pass.

Example: what the model sees (model context) vs. what the host sends when calling the tool

```json theme={null}
// Model context (what the model sees)
{
  "name": "get_shipment",
  "description": "Retrieve shipment status by reference",
  "inputSchema": {
    "type": "object",
    "properties": {
      "shipment": { "type": "string" }
    },
    "required": ["shipment"]
  }
}

// Host sends to server when calling the tool
{
  "method": "tools/call",
  "params": {
    "name": "get_shipment",
    "arguments": {
      "shipment": "PL-4417"
    }
  }
}
```

Because every call includes the tool name, arguments, and the relevant schema, servers can be swapped or upgraded without the user noticing — provided the schemas remain compatible.

References and further reading

* JSON Schema specification: [https://json-schema.org/](https://json-schema.org/)
* Designing secure server-side validation: [https://owasp.org/www-project-top-ten/](https://owasp.org/www-project-top-ten/)
* Best practices for API schemas and contracts (general guidance)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/model-context-protocol-associate-mcpa-certification/module/282d97e2-04f7-4beb-8301-695ecb3f3f99/lesson/573f6cff-2eac-4fb2-91c5-509efcf95189)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.