Merge pull request #18 from modelcontextprotocol/justin/concepts

Revise "Concepts" section, stub tabs to switch to Python
This commit is contained in:
Justin Spahr-Summers 2024-11-21 15:52:48 +00:00 • committed by GitHub
commit 1363e2c60a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 681 additions and 706 deletions

View file

@ -3,49 +3,69 @@ title: "Core architecture"
description: "Understand how MCP connects clients, servers, and LLMs" description: "Understand how MCP connects clients, servers, and LLMs"
--- ---
The Model Context Protocol (MCP) is built on a flexible, extensible architecture that enables seamless communication between LLM applications and external services. This document covers the core architectural components and concepts. The Model Context Protocol (MCP) is built on a flexible, extensible architecture that enables seamless communication between LLM applications and integrations. This document covers the core architectural components and concepts.
## Overview ## Overview
MCP follows a client-server architecture where: MCP follows a client-server architecture where:
- **Clients** are LLM applications (like claude.ai or IDEs) that initiate connections - **Hosts** are LLM applications (like Claude Desktop or IDEs) that initiate connections
- **Clients** maintain 1:1 connections with servers, inside the host application
- **Servers** provide context, tools, and prompts to clients - **Servers** provide context, tools, and prompts to clients
- **Hosts** are processes that run MCP clients
```mermaid ```mermaid
flowchart LR flowchart LR
subgraph "Host (e.g., claude.ai)" subgraph " Host (e.g., Claude Desktop) "
client[MCP Client] client1[MCP Client]
client2[MCP Client]
end end
subgraph "Server Process" subgraph "Server Process"
server[MCP Server] server1[MCP Server]
end end
subgraph "Server Process"
client <-->|Transport Layer| server server2[MCP Server]
end
client1 <-->|Transport Layer| server1
client2 <-->|Transport Layer| server2
``` ```
## Core components ## Core components
### Protocol layer ### Protocol layer
The protocol layer handles message framing, request/response linking, and high-level communication patterns. Key classes include: The protocol layer handles message framing, request/response linking, and high-level communication patterns.
```typescript <Tabs>
class Protocol<SendRequestT, SendNotificationT, SendResultT> { <Tab title="TypeScript">
// Handle incoming requests ```typescript
setRequestHandler<T>(schema: T, handler: (request: T) => Result): void class Protocol<Request, Notification, Result> {
// Handle incoming requests
// Handle incoming notifications setRequestHandler<T>(schema: T, handler: (request: T, extra: RequestHandlerExtra) => Promise<Result>): void
setNotificationHandler<T>(schema: T, handler: (notification: T) => void): void
// Handle incoming notifications
// Send requests and await responses setNotificationHandler<T>(schema: T, handler: (notification: T) => Promise<void>): void
request<T>(request: Request, schema: T): Promise<T>
// Send requests and await responses
// Send one-way notifications request<T>(request: Request, schema: T, options?: RequestOptions): Promise<T>
notification(notification: Notification): Promise<void>
} // Send one-way notifications
``` notification(notification: Notification): Promise<void>
}
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
Key classes include:
* `Protocol`
* `Client`
* `Server`
### Transport layer ### Transport layer
@ -54,66 +74,48 @@ The transport layer handles the actual communication between clients and servers
1. **Stdio transport** 1. **Stdio transport**
- Uses standard input/output for communication - Uses standard input/output for communication
- Ideal for local processes - Ideal for local processes
```typescript
class StdioTransport implements Transport {
constructor(stdin: Readable, stdout: Writable)
send(message: JSONRPCMessage): Promise<void>
}
```
2. **WebSocket transport** 2. **HTTP with SSE transport**
- Enables real-time bidirectional communication
- Supports remote connections
```typescript
class WebSocketTransport implements Transport {
constructor(url: URL)
send(message: JSONRPCMessage): Promise<void>
}
```
3. **SSE transport**
- Uses Server-Sent Events for server-to-client messages - Uses Server-Sent Events for server-to-client messages
- HTTP POST for client-to-server messages - HTTP POST for client-to-server messages
```typescript
class SSETransport implements Transport { All transports use [JSON-RPC](https://www.jsonrpc.org/) 2.0 to exchange messages. See the [specification](https://spec.modelcontextprotocol.io) for detailed information about the Model Context Protocol message format.
constructor(url: URL)
send(message: JSONRPCMessage): Promise<void>
}
```
### Message types ### Message types
MCP uses JSON-RPC 2.0 as its message format with three main types: MCP has these main types of messages:
1. **Requests** 1. **Requests** expect a response from the other side:
```typescript ```typescript
interface Request { interface Request {
method: string method: string;
params?: { params?: { ... };
_meta?: { }
progressToken?: string | number ```
}
}
}
```
2. **Notifications** 2. **Notifications** are one-way messages that don't expect a response:
```typescript ```typescript
interface Notification { interface Notification {
method: string method: string;
params?: { params?: { ... };
_meta?: Record<string, unknown> }
} ```
}
```
3. **Results** 3. **Results** are successful responses to requests:
```typescript ```typescript
interface Result { interface Result {
_meta?: Record<string, unknown> [key: string]: unknown;
[key: string]: unknown }
} ```
```
4. **Errors** indicate that a request failed:
```typescript
interface Error {
code: number;
message: string;
data?: unknown;
}
```
## Connection lifecycle ## Connection lifecycle
@ -123,27 +125,25 @@ MCP uses JSON-RPC 2.0 as its message format with three main types:
sequenceDiagram sequenceDiagram
participant Client participant Client
participant Server participant Server
Client->>Server: initialize request Client->>Server: initialize request
Server->>Client: initialize response Server->>Client: initialize response
Client->>Server: initialized notification Client->>Server: initialized notification
Note over Client,Server: Connection ready for use Note over Client,Server: Connection ready for use
``` ```
1. Client sends `initialize` request with capabilities 1. Client sends `initialize` request with protocol version and capabilities
2. Server responds with supported features 2. Server responds with its protocol version and capabilities
3. Client sends `initialized` notification 3. Client sends `initialized` notification as acknowledgment
4. Normal message exchange begins 4. Normal message exchange begins
### 2. Message exchange ### 2. Message exchange
After initialization, the following patterns are supported: After initialization, the following patterns are supported:
- **Request-Response**: Client sends request, server responds - **Request-Response**: Client or server sends requests, the other responds
- **Notifications**: Either party sends one-way messages - **Notifications**: Either party sends one-way messages
- **Progress Updates**: Long-running operations can report progress
- **Resource Updates**: Servers notify about changed resources
### 3. Termination ### 3. Termination
@ -154,19 +154,21 @@ Either party can terminate the connection:
## Error handling ## Error handling
MCP defines standard error codes and handling: MCP defines these standard error codes:
```typescript ```typescript
enum ErrorCode { enum ErrorCode {
ConnectionClosed = -1, // Standard JSON-RPC error codes
ParseError = -32700, ParseError = -32700,
InvalidRequest = -32600, InvalidRequest = -32600,
MethodNotFound = -32601, MethodNotFound = -32601,
InvalidParams = -32602, InvalidParams = -32602,
InternalError = -32603 InternalError = -32603
} }
``` ```
SDKs and applications can define their own error codes above -32000.
Errors are propagated through: Errors are propagated through:
- Error responses to requests - Error responses to requests
- Error events on transports - Error events on transports
@ -176,31 +178,44 @@ Errors are propagated through:
Here's a basic example of implementing an MCP server: Here's a basic example of implementing an MCP server:
```typescript <Tabs>
import { Server } from "@modelcontextprotocol/sdk/server"; <Tab title="TypeScript">
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio"; ```typescript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server({ const server = new Server({
name: "example-server", name: "example-server",
version: "1.0.0" version: "1.0.0"
}); }, {
capabilities: {
resources: {}
}
});
// Handle requests // Handle requests
server.setRequestHandler(ListResourcesRequestSchema, async () => { server.setRequestHandler(ListResourcesRequestSchema, async () => {
return { return {
resources: [ resources: [
{ {
uri: "example://resource", uri: "example://resource",
name: "Example Resource" name: "Example Resource"
} }
] ]
}; };
}); });
// Connect transport // Connect transport
const transport = new StdioServerTransport(); const transport = new StdioServerTransport();
await server.connect(transport); await server.connect(transport);
``` ```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Best practices ## Best practices
@ -212,9 +227,8 @@ await server.connect(transport);
- Simple process management - Simple process management
2. **Remote communication** 2. **Remote communication**
- WebSocket for full-duplex communication - Use SSE for scenarios requiring HTTP compatibility
- SSE for scenarios requiring HTTP compatibility - Consider security implications including authentication and authorization
- Consider security implications
### Message handling ### Message handling
@ -277,4 +291,4 @@ await server.connect(transport);
- Test different transports - Test different transports
- Verify error handling - Verify error handling
- Check edge cases - Check edge cases
- Load test servers - Load test servers

View file

@ -3,7 +3,11 @@ title: "Prompts"
description: "Create resuable prompt templates and workflows" description: "Create resuable prompt templates and workflows"
--- ---
MCP Prompts enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. They provide a powerful way to standardize and share common LLM interactions. Prompts enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. They provide a powerful way to standardize and share common LLM interactions.
<Note>
Prompts are designed to be **user-controlled**, meaning they are exposed from servers to clients with the intention of the user being able to explicitly select them for use.
</Note>
## Overview ## Overview
@ -20,13 +24,13 @@ Each prompt is defined with:
```typescript ```typescript
{ {
name: string; // Unique identifier for the prompt name: string; // Unique identifier for the prompt
description?: string; // Human-readable description description?: string; // Human-readable description
arguments?: [ // Optional list of arguments arguments?: [ // Optional list of arguments
{ {
name: string; // Argument identifier name: string; // Argument identifier
description?: string; // Argument description description?: string; // Argument description
required?: boolean; // Whether argument is required required?: boolean; // Whether argument is required
} }
] ]
} }
@ -78,15 +82,13 @@ To use a prompt, clients make a `prompts/get` request:
// Response // Response
{ {
description?: string, // Optional context about the prompt description: "Analyze Python code for potential improvements",
messages: [ // Messages to send to the LLM messages: [
{ {
role: "user" | "assistant", role: "user",
content: { content: {
type: "text" | "image", type: "text",
text?: string, text: "Please analyze the following Python code for potential improvements:\n\n```python\ndef calculate_sum(numbers):\n total = 0\n for num in numbers:\n total = total + num\n return total\n\nresult = calculate_sum([1, 2, 3, 4, 5])\nprint(result)\n```"
data?: string, // base64 for images
mimeType?: string
} }
} }
] ]
@ -97,57 +99,63 @@ To use a prompt, clients make a `prompts/get` request:
Prompts can be dynamic and include: Prompts can be dynamic and include:
### Context integration ### Embedded resource context
```typescript ```json
const analyzeLogsPrompt = { {
name: "analyze-logs", "name": "analyze-project",
description: "Analyze recent system logs", "description": "Analyze project logs and code",
arguments: [ "arguments": [
{ {
name: "timeframe", "name": "timeframe",
description: "Time period to analyze", "description": "Time period to analyze logs",
required: true "required": true
},
{
"name": "fileUri",
"description": "URI of code file to review",
"required": true
} }
] ]
}; }
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
if (request.params.name === "analyze-logs") {
const logs = await getSystemLogs(request.params.arguments?.timeframe);
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Analyze these system logs and identify any issues:\n\n${logs}`
}
}
]
};
}
});
``` ```
### Resource references When handling the `prompts/get` request:
```typescript ```json
const codeReviewPrompt = { {
name: "review-code", "messages": [
async getMessages(fileUri: string) { {
const code = await readResource(fileUri); "role": "user",
return [ "content": {
{ "type": "text",
role: "user", "text": "Analyze these system logs and the code file for any issues:"
content: { }
type: "text", },
text: `Review this code for best practices:\n\n${code}` {
"role": "user",
"content": {
"type": "resource",
"resource": {
"uri": "logs://recent?timeframe=1h",
"text": "[2024-03-14 15:32:11] ERROR: Connection timeout in network.py:127\n[2024-03-14 15:32:15] WARN: Retrying connection (attempt 2/3)\n[2024-03-14 15:32:20] ERROR: Max retries exceeded",
"mimeType": "text/plain"
} }
} }
]; },
} {
}; "role": "user",
"content": {
"type": "resource",
"resource": {
"uri": "file:///path/to/code.py",
"text": "def connect_to_service(timeout=30):\n retries = 3\n for attempt in range(retries):\n try:\n return establish_connection(timeout)\n except TimeoutError:\n if attempt == retries - 1:\n raise\n time.sleep(5)\n\ndef establish_connection(timeout):\n # Connection implementation\n pass",
"mimeType": "text/x-python"
}
}
}
]
}
``` ```
### Multi-step workflows ### Multi-step workflows
@ -187,94 +195,107 @@ const debugWorkflow = {
Here's a complete example of implementing prompts in an MCP server: Here's a complete example of implementing prompts in an MCP server:
```typescript <Tabs>
import { Server } from "@modelcontextprotocol/sdk/server"; <Tab title="TypeScript">
import { ```typescript
ListPromptsRequestSchema, import { Server } from "@modelcontextprotocol/sdk/server";
GetPromptRequestSchema import {
} from "@modelcontextprotocol/sdk/types"; ListPromptsRequestSchema,
GetPromptRequestSchema
} from "@modelcontextprotocol/sdk/types";
const PROMPTS = { const PROMPTS = {
"git-commit": { "git-commit": {
name: "git-commit", name: "git-commit",
description: "Generate a Git commit message", description: "Generate a Git commit message",
arguments: [ arguments: [
{ {
name: "changes", name: "changes",
description: "Git diff or description of changes", description: "Git diff or description of changes",
required: true required: true
} }
] ]
},
"explain-code": {
name: "explain-code",
description: "Explain how code works",
arguments: [
{
name: "code",
description: "Code to explain",
required: true
}, },
{ "explain-code": {
name: "language", name: "explain-code",
description: "Programming language", description: "Explain how code works",
required: false arguments: [
{
name: "code",
description: "Code to explain",
required: true
},
{
name: "language",
description: "Programming language",
required: false
}
]
} }
]
}
};
const server = new Server({
name: "example-prompts-server",
version: "1.0.0"
});
// List available prompts
server.setRequestHandler(ListPromptsRequestSchema, async () => {
return {
prompts: Object.values(PROMPTS)
};
});
// Get specific prompt
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
const prompt = PROMPTS[request.params.name];
if (!prompt) {
throw new Error(`Prompt not found: ${request.params.name}`);
}
if (request.params.name === "git-commit") {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Generate a concise but descriptive commit message for these changes:\n\n${request.params.arguments?.changes}`
}
}
]
}; };
}
if (request.params.name === "explain-code") { const server = new Server({
const language = request.params.arguments?.language || "Unknown"; name: "example-prompts-server",
return { version: "1.0.0"
messages: [ }, {
{ capabilities: {
role: "user", prompts: {}
content: { }
type: "text", });
text: `Explain how this ${language} code works:\n\n${request.params.arguments?.code}`
} // List available prompts
} server.setRequestHandler(ListPromptsRequestSchema, async () => {
] return {
}; prompts: Object.values(PROMPTS)
} };
});
throw new Error("Prompt implementation not found");
}); // Get specific prompt
``` server.setRequestHandler(GetPromptRequestSchema, async (request) => {
const prompt = PROMPTS[request.params.name];
if (!prompt) {
throw new Error(`Prompt not found: ${request.params.name}`);
}
if (request.params.name === "git-commit") {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Generate a concise but descriptive commit message for these changes:\n\n${request.params.arguments?.changes}`
}
}
]
};
}
if (request.params.name === "explain-code") {
const language = request.params.arguments?.language || "Unknown";
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Explain how this ${language} code works:\n\n${request.params.arguments?.code}`
}
}
]
};
}
throw new Error("Prompt implementation not found");
});
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Best practices ## Best practices
@ -324,70 +345,3 @@ When implementing prompts:
- Implement timeouts - Implement timeouts
- Consider prompt injection risks - Consider prompt injection risks
- Document security requirements - Document security requirements
## Advanced features
### Prompt chaining
```typescript
const analyzePRPrompt = {
async getMessages(prId: string) {
// Chain multiple prompts
const diffPrompt = await getPrompt("analyze-diff");
const testsPrompt = await getPrompt("review-tests");
const securityPrompt = await getPrompt("security-review");
return [
...diffPrompt.messages,
...testsPrompt.messages,
...securityPrompt.messages
];
}
};
```
### Context enrichment
```typescript
const troubleshootPrompt = {
async getMessages(error: string) {
// Enrich with system context
const systemInfo = await getSystemInfo();
const recentLogs = await getRecentLogs();
const stackTrace = await parseError(error);
return [
{
role: "user",
content: {
type: "text",
text: `Help troubleshoot this error:
Error: ${error}
System: ${systemInfo}
Logs: ${recentLogs}
Stack: ${stackTrace}`
}
}
];
}
};
```
### Interactive workflows
```typescript
const debugWorkflow = {
async getMessages(stage: string, context: any) {
switch (stage) {
case "initial":
return promptForError();
case "analysis":
return analyzeError(context.error);
case "solution":
return suggestFixes(context.analysis);
case "verification":
return verifyFix(context.solution);
}
}
};
```

View file

@ -5,6 +5,12 @@ description: "Expose data and content from your servers to LLMs"
Resources are a core primitive in the Model Context Protocol (MCP) that allow servers to expose data and content that can be read by clients and used as context for LLM interactions. Resources are a core primitive in the Model Context Protocol (MCP) that allow servers to expose data and content that can be read by clients and used as context for LLM interactions.
<Note>
Resources are designed to be **application-controlled**, meaning that the client application can decide how and when they should be used.
For example, one application may require users to explicitly select resources, while another could automatically select them based on heuristics or even at the discretion of the AI model itself.
</Note>
## Overview ## Overview
Resources represent any kind of data that an MCP server wants to make available to clients. This can include: Resources represent any kind of data that an MCP server wants to make available to clients. This can include:
@ -24,7 +30,7 @@ Each resource is identified by a unique URI and can contain either text or binar
Resources are identified using URIs that follow this format: Resources are identified using URIs that follow this format:
``` ```
[protocol]://[path] [protocol]://[host]/[path]
``` ```
For example: For example:
@ -42,7 +48,7 @@ Resources can contain two types of content:
Text resources contain UTF-8 encoded text data. These are suitable for: Text resources contain UTF-8 encoded text data. These are suitable for:
- Source code - Source code
- Configuration files - Configuration files
- Log files - Log files
- JSON/XML data - JSON/XML data
- Plain text - Plain text
@ -66,37 +72,39 @@ Servers expose a list of concrete resources via the `resources/list` endpoint. E
```typescript ```typescript
{ {
uri: string; // Unique identifier for the resource uri: string; // Unique identifier for the resource
name: string; // Human-readable name name: string; // Human-readable name
description?: string; // Optional description description?: string; // Optional description
mimeType?: string; // Optional MIME type mimeType?: string; // Optional MIME type
} }
``` ```
### Resource templates ### Resource templates
For dynamic resources, servers can expose URI templates that clients can use to construct valid resource URIs: For dynamic resources, servers can expose [URI templates](https://datatracker.ietf.org/doc/html/rfc6570) that clients can use to construct valid resource URIs:
```typescript ```typescript
{ {
uriTemplate: string; // URI template following RFC 6570 uriTemplate: string; // URI template following RFC 6570
name: string; // Human-readable name for this type name: string; // Human-readable name for this type
description?: string; // Optional description description?: string; // Optional description
mimeType?: string; // Optional MIME type for all matching resources mimeType?: string; // Optional MIME type for all matching resources
} }
``` ```
## Reading resources ## Reading resources
To read a resource, clients make a `resources/read` request with the resource URI. The server responds with: To read a resource, clients make a `resources/read` request with the resource URI.
The server responds with a list of resource contents:
```typescript ```typescript
{ {
contents: [ contents: [
{ {
uri: string; // The requested URI uri: string; // The URI of the resource
mimeType?: string; // Optional MIME type mimeType?: string; // Optional MIME type
// One of: // One of:
text?: string; // For text resources text?: string; // For text resources
blob?: string; // For binary resources (base64 encoded) blob?: string; // For binary resources (base64 encoded)
@ -105,6 +113,10 @@ To read a resource, clients make a `resources/read` request with the resource UR
} }
``` ```
<Tip>
Servers may return multiple resources in response to one `resources/read` request. This could be used, for example, to return a list of files inside a directory when the directory is read.
</Tip>
## Resource updates ## Resource updates
MCP supports real-time updates for resources through two mechanisms: MCP supports real-time updates for resources through two mechanisms:
@ -113,7 +125,7 @@ MCP supports real-time updates for resources through two mechanisms:
Servers can notify clients when their list of available resources changes via the `notifications/resources/list_changed` notification. Servers can notify clients when their list of available resources changes via the `notifications/resources/list_changed` notification.
### Content changes ### Content changes
Clients can subscribe to updates for specific resources: Clients can subscribe to updates for specific resources:
@ -126,45 +138,58 @@ Clients can subscribe to updates for specific resources:
Here's a simple example of implementing resource support in an MCP server: Here's a simple example of implementing resource support in an MCP server:
```typescript <Tabs>
const server = new Server({ <Tab title="TypeScript">
name: "example-server", ```typescript
version: "1.0.0" const server = new Server({
}); name: "example-server",
version: "1.0.0"
// List available resources }, {
server.setRequestHandler(ListResourcesRequestSchema, async () => { capabilities: {
return { resources: {}
resources: [
{
uri: "file:///logs/app.log",
name: "Application Logs",
mimeType: "text/plain"
} }
] });
};
});
// Read resource contents // List available resources
server.setRequestHandler(ReadResourceRequestSchema, async (request) => { server.setRequestHandler(ListResourcesRequestSchema, async () => {
const uri = request.params.uri; return {
resources: [
if (uri === "file:///logs/app.log") { {
const logContents = await readLogFile(); uri: "file:///logs/app.log",
return { name: "Application Logs",
contents: [ mimeType: "text/plain"
{ }
uri, ]
mimeType: "text/plain", };
text: logContents });
}
] // Read resource contents
}; server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
} const uri = request.params.uri;
throw new Error("Resource not found"); if (uri === "file:///logs/app.log") {
}); const logContents = await readLogFile();
``` return {
contents: [
{
uri,
mimeType: "text/plain",
text: logContents
}
]
};
}
throw new Error("Resource not found");
});
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Best practices ## Best practices
@ -194,4 +219,4 @@ When exposing resources:
- Encrypt sensitive data in transit - Encrypt sensitive data in transit
- Validate MIME types - Validate MIME types
- Implement timeouts for long-running reads - Implement timeouts for long-running reads
- Handle resource cleanup appropriately - Handle resource cleanup appropriately

View file

@ -5,6 +5,10 @@ description: "Let your servers request completions from LLMs"
Sampling is a powerful MCP feature that allows servers to request LLM completions through the client, enabling sophisticated agentic behaviors while maintaining security and privacy. Sampling is a powerful MCP feature that allows servers to request LLM completions through the client, enabling sophisticated agentic behaviors while maintaining security and privacy.
<Info>
This feature of MCP is not yet supported in the Claude Desktop client.
</Info>
## How sampling works ## How sampling works
The sampling flow follows these steps: The sampling flow follows these steps:
@ -12,7 +16,7 @@ The sampling flow follows these steps:
1. Server sends a `sampling/createMessage` request to the client 1. Server sends a `sampling/createMessage` request to the client
2. Client reviews the request and can modify it 2. Client reviews the request and can modify it
3. Client samples from an LLM 3. Client samples from an LLM
4. Client reviews the completion 4. Client reviews the completion
5. Client returns the result to the server 5. Client returns the result to the server
This human-in-the-loop design ensures users maintain control over what the LLM sees and generates. This human-in-the-loop design ensures users maintain control over what the LLM sees and generates.
@ -28,14 +32,24 @@ Sampling requests use a standardized message format:
role: "user" | "assistant", role: "user" | "assistant",
content: { content: {
type: "text" | "image", type: "text" | "image",
// For text: // For text:
text?: string, text?: string,
// For images: // For images:
data?: string, // base64 encoded data?: string, // base64 encoded
mimeType?: string mimeType?: string
} }
} }
], ],
modelPreferences?: {
hints?: [{
name?: string // Suggested model name/family
}],
costPriority?: number, // 0-1, importance of minimizing cost
speedPriority?: number, // 0-1, importance of low latency
intelligencePriority?: number // 0-1, importance of capabilities
},
systemPrompt?: string, systemPrompt?: string,
includeContext?: "none" | "thisServer" | "allServers", includeContext?: "none" | "thisServer" | "allServers",
temperature?: number, temperature?: number,
@ -51,11 +65,27 @@ Sampling requests use a standardized message format:
The `messages` array contains the conversation history to send to the LLM. Each message has: The `messages` array contains the conversation history to send to the LLM. Each message has:
- `role`: Either "user" or "assistant" - `role`: Either "user" or "assistant"
- `content`: The message content, which can be: - `content`: The message content, which can be:
- Text content with a `text` field - Text content with a `text` field
- Image content with `data` (base64) and `mimeType` fields - Image content with `data` (base64) and `mimeType` fields
### Model preferences
The `modelPreferences` object allows servers to specify their model selection preferences:
- `hints`: Array of model name suggestions that clients can use to select an appropriate model:
- `name`: String that can match full or partial model names (e.g. "claude-3", "sonnet")
- Clients may map hints to equivalent models from different providers
- Multiple hints are evaluated in preference order
- Priority values (0-1 normalized):
- `costPriority`: Importance of minimizing costs
- `speedPriority`: Importance of low latency response
- `intelligencePriority`: Importance of advanced model capabilities
Clients make the final model selection based on these preferences and their available models.
### System prompt ### System prompt
An optional `systemPrompt` field allows servers to request a specific system prompt. The client may modify or ignore this. An optional `systemPrompt` field allows servers to request a specific system prompt. The client may modify or ignore this.
@ -85,8 +115,8 @@ The client returns a completion result:
```typescript ```typescript
{ {
model: string, // Name of the model used model: string, // Name of the model used
stopReason: "endTurn" | "stopSequence" | "maxTokens", stopReason?: "endTurn" | "stopSequence" | "maxTokens" | string,
role: "user" | "assistant", role: "user" | "assistant",
content: { content: {
type: "text" | "image", type: "text" | "image",
@ -97,33 +127,27 @@ The client returns a completion result:
} }
``` ```
## Example implementation ## Example request
Here's an example of requesting sampling from a client: Here's an example of requesting sampling from a client:
```json
```typescript {
// Server-side code "method": "sampling/createMessage",
const response = await server.request({ "params": {
method: "sampling/createMessage", "messages": [
params: {
messages: [
{ {
role: "user", "role": "user",
content: { "content": {
type: "text", "type": "text",
text: "What files are in the current directory?" "text": "What files are in the current directory?"
} }
} }
], ],
systemPrompt: "You are a helpful file system assistant.", "systemPrompt": "You are a helpful file system assistant.",
includeContext: "thisServer", "includeContext": "thisServer",
maxTokens: 100, "maxTokens": 100
temperature: 0.7
} }
}, CreateMessageResultSchema); }
// Handle the completion
console.log(response.content.text);
``` ```
## Best practices ## Best practices
@ -218,4 +242,4 @@ Be aware of these limitations:
- Costs should be considered - Costs should be considered
- Model availability varies - Model availability varies
- Response times vary - Response times vary
- Not all content types supported - Not all content types supported

View file

@ -5,14 +5,19 @@ description: "Enable LLMs to perform actions through your server"
Tools are a powerful primitive in the Model Context Protocol (MCP) that enable servers to expose executable functionality to clients. Through tools, LLMs can interact with external systems, perform computations, and take actions in the real world. Tools are a powerful primitive in the Model Context Protocol (MCP) that enable servers to expose executable functionality to clients. Through tools, LLMs can interact with external systems, perform computations, and take actions in the real world.
## How tools work <Note>
Tools are designed to be **model-controlled**, meaning that tools are exposed from servers to clients with the intention of the AI model being able to automatically invoke them (with a human in the loop to grant approval).
</Note>
Tools in MCP follow a request-response pattern where: ## Overview
1. Clients discover available tools through the `tools/list` endpoint Tools in MCP allow servers to expose executable functions that can be invoked by clients and used by LLMs to perform actions. Key aspects of tools include:
2. Clients can invoke tools using the `tools/call` endpoint
3. Servers execute the requested operation and return results - **Discovery**: Clients can list available tools through the `tools/list` endpoint
4. Progress updates can be sent during long-running operations - **Invocation**: Tools are called using the `tools/call` endpoint, where servers perform the requested operation and return results
- **Flexibility**: Tools can range from simple calculations to complex API interactions
Like [resources](/docs/concepts/resources), tools are identified by unique names and can include descriptions to guide their usage. However, unlike resources, tools represent dynamic operations that can modify state or interact with external systems.
## Tool definition structure ## Tool definition structure
@ -20,13 +25,11 @@ Each tool is defined with the following structure:
```typescript ```typescript
{ {
name: string; // Unique identifier for the tool name: string; // Unique identifier for the tool
description?: string; // Human-readable description description?: string; // Human-readable description
inputSchema: { // JSON Schema for the tool's parameters inputSchema: { // JSON Schema for the tool's parameters
type: "object", type: "object",
properties: { properties: { ... } // Tool-specific parameters
// Tool-specific parameters
}
} }
} }
``` ```
@ -35,73 +38,58 @@ Each tool is defined with the following structure:
Here's an example of implementing a basic tool in an MCP server: Here's an example of implementing a basic tool in an MCP server:
```typescript <Tabs>
const server = new Server({ <Tab title="TypeScript">
name: "example-server", ```typescript
version: "1.0.0" const server = new Server({
}); name: "example-server",
version: "1.0.0"
// Define available tools }, {
server.setRequestHandler(ListToolsRequestSchema, async () => { capabilities: {
return { tools: {}
tools: [{
name: "calculate_sum",
description: "Add two numbers together",
inputSchema: {
type: "object",
properties: {
a: { type: "number" },
b: { type: "number" }
},
required: ["a", "b"]
} }
}] });
};
});
// Handle tool execution // Define available tools
server.setRequestHandler(CallToolRequestSchema, async (request) => { server.setRequestHandler(ListToolsRequestSchema, async () => {
if (request.params.name === "calculate_sum") { return {
const { a, b } = request.params.arguments; tools: [{
return { name: "calculate_sum",
toolResult: a + b description: "Add two numbers together",
}; inputSchema: {
} type: "object",
throw new Error("Tool not found"); properties: {
}); a: { type: "number" },
``` b: { type: "number" }
},
required: ["a", "b"]
}
}]
};
});
## Progress reporting // Handle tool execution
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "calculate_sum") {
const { a, b } = request.params.arguments;
return {
toolResult: a + b
};
}
throw new Error("Tool not found");
});
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
For long-running operations, tools can report progress using the progress notification system: ## Example tool patterns
```typescript Here are some examples of types of tools that a server could provide:
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "long_process") {
const progressToken = request.params._meta?.progressToken;
for (let i = 0; i < total; i++) {
await doWork();
// Send progress update
await server.notification({
method: "notifications/progress",
params: {
progress: i,
total: total,
progressToken
}
});
}
return { toolResult: "Complete" };
}
});
```
## Common tool patterns
Here are some common patterns for implementing tools:
### System operations ### System operations
@ -121,7 +109,7 @@ Tools that interact with the local system:
} }
``` ```
### API integrations ### API integrations
Tools that wrap external APIs: Tools that wrap external APIs:
@ -152,10 +140,10 @@ Tools that transform or analyze data:
type: "object", type: "object",
properties: { properties: {
filepath: { type: "string" }, filepath: { type: "string" },
operations: { operations: {
type: "array", type: "array",
items: { items: {
enum: ["sum", "average", "count"] enum: ["sum", "average", "count"]
} }
} }
} }
@ -169,7 +157,7 @@ When implementing tools:
1. Provide clear, descriptive names and descriptions 1. Provide clear, descriptive names and descriptions
2. Use detailed JSON Schema definitions for parameters 2. Use detailed JSON Schema definitions for parameters
3. Include examples in tool descriptions when helpful 3. Include examples in tool descriptions to demonstrate how the model should use them
4. Implement proper error handling and validation 4. Implement proper error handling and validation
5. Use progress reporting for long operations 5. Use progress reporting for long operations
6. Keep tool operations focused and atomic 6. Keep tool operations focused and atomic
@ -215,65 +203,48 @@ MCP supports dynamic tool discovery:
3. Tools can be added or removed during runtime 3. Tools can be added or removed during runtime
4. Tool definitions can be updated (though this should be done carefully) 4. Tool definitions can be updated (though this should be done carefully)
## Common tool categories
Tools typically fall into these categories:
### System integration
- File operations
- Process management
- System information
- Network operations
### External services
- API calls
- Database operations
- Authentication services
- Cloud services
### Data processing
- Format conversion
- Data analysis
- Content generation
- Media processing
### Device control
- Hardware interaction
- Device configuration
- Sensor reading
- Output control
## Error handling ## Error handling
Tools should return clear error information: Tool errors should be reported within the result object, not as MCP protocol-level errors. This allows the LLM to see and potentially handle the error. When a tool encounters an error:
1. Set `isError` to `true` in the result
2. Include error details in the `content` array
Here's an example of proper error handling for tools:
```typescript ```typescript
try { try {
// Tool operation // Tool operation
const result = performOperation();
return {
content: [
{
type: "text",
text: `Operation successful: ${result}`
}
]
};
} catch (error) { } catch (error) {
return { return {
toolResult: { isError: true,
error: { content: [
code: "OPERATION_FAILED", {
message: "Failed to process request", type: "text",
details: error.message text: `Error: ${error.message}`
} }
} ]
}; };
} }
``` ```
This approach allows the LLM to see that an error occurred and potentially take corrective action or request human intervention.
## Testing tools ## Testing tools
Consider these testing approaches: A comprehensive testing strategy for MCP tools should cover:
1. Unit test parameter validation - **Functional testing**: Verify tools execute correctly with valid inputs and handle invalid inputs appropriately
2. Mock external dependencies - **Integration testing**: Test tool interaction with external systems using both real and mocked dependencies
3. Test progress reporting - **Security testing**: Validate authentication, authorization, input sanitization, and rate limiting
4. Verify error handling - **Performance testing**: Check behavior under load, timeout handling, and resource cleanup
5. Test concurrent execution - **Error handling**: Ensure tools properly report errors through the MCP protocol and clean up resources
6. Validate return values
7. Test rate limiting
8. Check resource cleanup
9. Test timeout handling
10. Verify security constraints

View file

@ -5,128 +5,13 @@ description: "Learn about MCP's communication mechanisms"
Transports in the Model Context Protocol (MCP) provide the foundation for communication between clients and servers. A transport handles the underlying mechanics of how messages are sent and received. Transports in the Model Context Protocol (MCP) provide the foundation for communication between clients and servers. A transport handles the underlying mechanics of how messages are sent and received.
## How transports work ## Message Format
An MCP transport is built on a simple message-passing architecture where both clients and servers can send messages to each other. The transport layer is responsible for: MCP uses [JSON-RPC](https://www.jsonrpc.org/) 2.0 as its wire format. The transport layer is responsible for converting MCP protocol messages into JSON-RPC format for transmission and converting received JSON-RPC messages back into MCP protocol messages.
- Establishing connections There are three types of JSON-RPC messages used:
- Sending messages
- Receiving messages
- Handling connection lifecycles
- Managing errors
- Ensuring reliable delivery
## Available transport types
MCP supports three main transport types:
### Standard input/output (stdio)
The stdio transport enables communication through standard input and output streams. This is particularly useful for local integrations and command-line tools.
```typescript
// Server implementation
const server = new Server({
name: "example-server",
version: "1.0.0"
});
const transport = new StdioServerTransport();
await server.connect(transport);
// Client implementation
const client = new Client({
name: "example-client",
version: "1.0.0"
});
const transport = new StdioClientTransport({
command: "./server",
args: ["--option", "value"]
});
await client.connect(transport);
```
### WebSocket
WebSocket transport provides real-time, bidirectional communication over a persistent connection. This is ideal for web-based applications that need low-latency messaging.
```typescript
// Server implementation
const server = new Server({
name: "example-server",
version: "1.0.0"
});
const transport = new WebSocketServerTransport();
await server.connect(transport);
// Client implementation
const client = new Client({
name: "example-client",
version: "1.0.0"
});
const transport = new WebSocketClientTransport(
new URL("ws://localhost:3000")
);
await client.connect(transport);
```
### Server-sent events
SSE transport enables server-to-client streaming with HTTP POST requests for client-to-server communication. This is useful when WebSocket support isn't available.
```typescript
// Server implementation
const server = new Server({
name: "example-server",
version: "1.0.0"
});
const transport = new SSEServerTransport("/message", response);
await server.connect(transport);
// Client implementation
const client = new Client({
name: "example-client",
version: "1.0.0"
});
const transport = new SSEClientTransport(
new URL("http://localhost:3000/sse")
);
await client.connect(transport);
```
## Transport interface
All transport implementations must conform to the Transport interface:
```typescript
interface Transport {
// Start processing messages
start(): Promise<void>;
// Send a JSON-RPC message
send(message: JSONRPCMessage): Promise<void>;
// Close the connection
close(): Promise<void>;
// Callbacks
onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage) => void;
}
```
## Message format
Messages sent over MCP transport follow the JSON-RPC 2.0 specification. There are three types of messages:
### Requests ### Requests
```typescript ```typescript
{ {
jsonrpc: "2.0", jsonrpc: "2.0",
@ -137,7 +22,6 @@ Messages sent over MCP transport follow the JSON-RPC 2.0 specification. There ar
``` ```
### Responses ### Responses
```typescript ```typescript
{ {
jsonrpc: "2.0", jsonrpc: "2.0",
@ -152,7 +36,6 @@ Messages sent over MCP transport follow the JSON-RPC 2.0 specification. There ar
``` ```
### Notifications ### Notifications
```typescript ```typescript
{ {
jsonrpc: "2.0", jsonrpc: "2.0",
@ -161,7 +44,130 @@ Messages sent over MCP transport follow the JSON-RPC 2.0 specification. There ar
} }
``` ```
## Error handling ## Built-in Transport Types
MCP includes two standard transport implementations:
### Standard Input/Output (stdio)
The stdio transport enables communication through standard input and output streams. This is particularly useful for local integrations and command-line tools.
Use stdio when:
- Building command-line tools
- Implementing local integrations
- Needing simple process communication
- Working with shell scripts
<Tabs>
<Tab title="TypeScript">
```typescript
// Server implementation
const server = new Server({
name: "example-server",
version: "1.0.0"
});
const transport = new StdioServerTransport();
await server.connect(transport);
// Client implementation
const client = new Client({
name: "example-client",
version: "1.0.0"
});
const transport = new StdioClientTransport({
command: "./server",
args: ["--option", "value"]
});
await client.connect(transport);
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
### Server-Sent Events (SSE)
SSE transport enables server-to-client streaming with HTTP POST requests for client-to-server communication.
Use SSE when:
- Only server-to-client streaming is needed
- Working with restricted networks
- Implementing simple updates
<Tabs>
<Tab title="TypeScript">
```typescript
// Server implementation
const server = new Server({
name: "example-server",
version: "1.0.0"
});
const transport = new SSEServerTransport("/message", response);
await server.connect(transport);
// Client implementation
const client = new Client({
name: "example-client",
version: "1.0.0"
});
const transport = new SSEClientTransport(
new URL("http://localhost:3000/sse")
);
await client.connect(transport);
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Custom Transports
MCP makes it easy to implement custom transports for specific needs. Any transport implementation just needs to conform to the Transport interface:
You can implement custom transports for:
- Custom network protocols
- Specialized communication channels
- Integration with existing systems
- Performance optimization
<Tabs>
<Tab title="TypeScript">
```typescript
interface Transport {
// Start processing messages
start(): Promise<void>;
// Send a JSON-RPC message
send(message: JSONRPCMessage): Promise<void>;
// Close the connection
close(): Promise<void>;
// Callbacks
onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage) => void;
}
```
</Tab>
<Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Error Handling
Transport implementations should handle various error scenarios: Transport implementations should handle various error scenarios:
@ -173,29 +179,38 @@ Transport implementations should handle various error scenarios:
Example error handling: Example error handling:
```typescript <Tabs>
class ExampleTransport implements Transport { <Tab title="TypeScript">
async start() { ```typescript
try { class ExampleTransport implements Transport {
// Connection logic async start() {
} catch (error) { try {
this.onerror?.(new Error(`Failed to connect: ${error}`)); // Connection logic
throw error; } catch (error) {
} this.onerror?.(new Error(`Failed to connect: ${error}`));
} throw error;
}
}
async send(message: JSONRPCMessage) { async send(message: JSONRPCMessage) {
try { try {
// Sending logic // Sending logic
} catch (error) { } catch (error) {
this.onerror?.(new Error(`Failed to send message: ${error}`)); this.onerror?.(new Error(`Failed to send message: ${error}`));
throw error; throw error;
}
}
} }
} ```
} </Tab>
``` <Tab title="Python">
```python
# Python implementation coming soon
```
</Tab>
</Tabs>
## Best practices ## Best Practices
When implementing or using MCP transport: When implementing or using MCP transport:
@ -210,34 +225,31 @@ When implementing or using MCP transport:
9. Monitor connection health 9. Monitor connection health
10. Implement proper security measures 10. Implement proper security measures
## Security considerations ## Security Considerations
When implementing transport: When implementing transport:
### Authentication and authorization ### Authentication and Authorization
- Implement proper authentication mechanisms - Implement proper authentication mechanisms
- Validate client credentials - Validate client credentials
- Use secure token handling - Use secure token handling
- Implement authorization checks - Implement authorization checks
### Data security ### Data Security
- Use TLS for network transport - Use TLS for network transport
- Encrypt sensitive data - Encrypt sensitive data
- Validate message integrity - Validate message integrity
- Implement message size limits - Implement message size limits
- Sanitize input data - Sanitize input data
### Network security ### Network Security
- Implement rate limiting - Implement rate limiting
- Use appropriate timeouts - Use appropriate timeouts
- Handle denial of service scenarios - Handle denial of service scenarios
- Monitor for unusual patterns - Monitor for unusual patterns
- Implement proper firewall rules - Implement proper firewall rules
## Debugging transport ## Debugging Transport
Tips for debugging transport issues: Tips for debugging transport issues:
@ -251,28 +263,3 @@ Tips for debugging transport issues:
8. Monitor resource usage 8. Monitor resource usage
9. Test edge cases 9. Test edge cases
10. Use proper error tracking 10. Use proper error tracking
## Transport selection guide
Choose the appropriate transport based on your needs:
### Use stdio when
- Building command-line tools
- Implementing local integrations
- Needing simple process communication
- Working with shell scripts
### Use WebSocket when
- Real-time updates are needed
- Low latency is required
- Bidirectional communication is necessary
- Working with web applications
### Use SSE when
- Only server-to-client streaming is needed
- WebSocket support is limited
- Working with restricted networks
- Implementing simple updates