Merge pull request #8 from modelcontextprotocol/zack-lee/qol
Quickstart QoL, grammar fixes
This commit is contained in:
commit
24b4608bbb
42 changed files with 634 additions and 4023 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Connection Management"
|
title: "Connection management"
|
||||||
description: "Learn how to establish and manage MCP client connections"
|
description: "Learn how to establish and manage MCP client connections"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Resource Consumption"
|
title: "Resource consumption"
|
||||||
description: "Learn how to consume resources from MCP servers"
|
description: "Learn how to consume resources from MCP servers"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Tool Invocation"
|
title: "Tool invocation"
|
||||||
description: "Learn how to invoke tools through the MCP client API"
|
description: "Learn how to invoke tools through the MCP client API"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: 'Error Handling'
|
title: 'Error handling'
|
||||||
description: 'How to handle and respond to errors in the Model Context Protocol'
|
description: 'How to handle and respond to errors in the Model Context Protocol'
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: 'Message Types'
|
title: 'Message types'
|
||||||
description: 'Understanding the different types of messages in the Model Context Protocol'
|
description: 'Understanding the different types of messages in the Model Context Protocol'
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Protocol Overview"
|
title: "Protocol overview"
|
||||||
description: "Learn about the core concepts and architecture of the Model Context Protocol (MCP)"
|
description: "Learn about the core concepts and architecture of the Model Context Protocol (MCP)"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -22,10 +22,8 @@ The Model Context Protocol (MCP) is a standardized protocol that enables AI appl
|
||||||
</Card>
|
</Card>
|
||||||
</CardGroup>
|
</CardGroup>
|
||||||
|
|
||||||
# Core concepts
|
## Core concepts
|
||||||
|
|
||||||
MCP defines several key primitives that form the foundation of the protocol:
|
MCP defines several key primitives that form the foundation of the protocol:
|
||||||
|
|
||||||
<CardGroup cols={2}>
|
<CardGroup cols={2}>
|
||||||
<Card title="Resources" icon="book">
|
<Card title="Resources" icon="book">
|
||||||
Data that can be loaded as context for the AI model. Resources have URIs and can contain text or binary data.
|
Data that can be loaded as context for the AI model. Resources have URIs and can contain text or binary data.
|
||||||
|
|
@ -41,12 +39,10 @@ MCP defines several key primitives that form the foundation of the protocol:
|
||||||
</Card>
|
</Card>
|
||||||
</CardGroup>
|
</CardGroup>
|
||||||
|
|
||||||
# Protocol flow
|
## Protocol flow
|
||||||
|
|
||||||
## Connection and initialization
|
|
||||||
|
|
||||||
|
### Connection and initialization
|
||||||
When a client connects to an MCP server, they go through the following initialization sequence:
|
When a client connects to an MCP server, they go through the following initialization sequence:
|
||||||
|
|
||||||
<Steps>
|
<Steps>
|
||||||
<Step title="Client connects">
|
<Step title="Client connects">
|
||||||
The client establishes a connection to the server using one of the supported transports.
|
The client establishes a connection to the server using one of the supported transports.
|
||||||
|
|
@ -64,10 +60,8 @@ When a client connects to an MCP server, they go through the following initializ
|
||||||
|
|
||||||
After initialization, the client can begin using the server's resources, tools, and other capabilities.
|
After initialization, the client can begin using the server's resources, tools, and other capabilities.
|
||||||
|
|
||||||
## Transport options
|
### Transport options
|
||||||
|
|
||||||
MCP supports multiple transport mechanisms for client-server communication:
|
MCP supports multiple transport mechanisms for client-server communication:
|
||||||
|
|
||||||
<AccordionGroup>
|
<AccordionGroup>
|
||||||
<Accordion title="Standard input/output (stdio)">
|
<Accordion title="Standard input/output (stdio)">
|
||||||
- Server process reads from stdin and writes to stdout
|
- Server process reads from stdin and writes to stdout
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
---
|
---
|
||||||
title: "Python Class Reference"
|
title: "Python class reference"
|
||||||
description: "Complete Python SDK class documentation"
|
description: "Complete Python SDK class documentation"
|
||||||
---
|
---
|
||||||
|
|
||||||
## Server Class
|
## Server class
|
||||||
|
|
||||||
The main class for implementing MCP servers in Python.
|
The main class for implementing MCP servers in Python.
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ Creates a new MCP server instance.
|
||||||
|
|
||||||
### Decorators
|
### Decorators
|
||||||
|
|
||||||
#### Resource Handling
|
#### Resource handling
|
||||||
|
|
||||||
##### @server.list_resources()
|
##### @server.list_resources()
|
||||||
```python
|
```python
|
||||||
|
|
@ -72,7 +72,7 @@ async def unsubscribe_resource(uri: str) -> None:
|
||||||
**Parameters:**
|
**Parameters:**
|
||||||
- `uri` (str): Resource URI to unsubscribe from
|
- `uri` (str): Resource URI to unsubscribe from
|
||||||
|
|
||||||
#### Tool Support
|
#### Tool support
|
||||||
|
|
||||||
##### @server.list_tools()
|
##### @server.list_tools()
|
||||||
```python
|
```python
|
||||||
|
|
@ -99,7 +99,7 @@ async def call_tool(name: str, **kwargs) -> Any:
|
||||||
|
|
||||||
**Returns:** Tool execution result (any JSON-serializable value)
|
**Returns:** Tool execution result (any JSON-serializable value)
|
||||||
|
|
||||||
#### Prompt Management
|
#### Prompt management
|
||||||
|
|
||||||
##### @server.list_prompts()
|
##### @server.list_prompts()
|
||||||
```python
|
```python
|
||||||
|
|
@ -128,7 +128,7 @@ async def get_prompt(name: str, arguments: dict) -> PromptResponse:
|
||||||
- `messages` (List[Message]): List of prompt messages
|
- `messages` (List[Message]): List of prompt messages
|
||||||
- `description` (str, optional): Description of the prompt response
|
- `description` (str, optional): Description of the prompt response
|
||||||
|
|
||||||
### Session Methods
|
### Session methods
|
||||||
|
|
||||||
Methods available on the server session object:
|
Methods available on the server session object:
|
||||||
|
|
||||||
|
|
@ -158,7 +158,7 @@ async def send_log_message(
|
||||||
"""Sends log messages to clients."""
|
"""Sends log messages to clients."""
|
||||||
```
|
```
|
||||||
|
|
||||||
## Data Classes
|
## Data classes
|
||||||
|
|
||||||
### Resource
|
### Resource
|
||||||
```python
|
```python
|
||||||
|
|
@ -204,7 +204,7 @@ class PromptResponse:
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example Usage
|
## Example usage
|
||||||
|
|
||||||
Here's a complete example demonstrating the main features:
|
Here's a complete example demonstrating the main features:
|
||||||
|
|
||||||
|
|
@ -263,7 +263,7 @@ if __name__ == "__main__":
|
||||||
anyio.run(main)
|
anyio.run(main)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Best Practices
|
## Best practices
|
||||||
|
|
||||||
1. **Error Handling**: Always implement proper error handling in your server methods:
|
1. **Error Handling**: Always implement proper error handling in your server methods:
|
||||||
```python
|
```python
|
||||||
|
|
@ -303,15 +303,15 @@ if __name__ == "__main__":
|
||||||
await session.send_resource_updated("example://resource1")
|
await session.send_resource_updated("example://resource1")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Transport Options
|
## Transport options
|
||||||
|
|
||||||
### stdio Transport
|
### Stdio transport
|
||||||
```python
|
```python
|
||||||
async with stdio_server() as (read_stream, write_stream):
|
async with stdio_server() as (read_stream, write_stream):
|
||||||
await server.run(read_stream, write_stream)
|
await server.run(read_stream, write_stream)
|
||||||
```
|
```
|
||||||
|
|
||||||
### SSE Transport
|
### SSE transport
|
||||||
```python
|
```python
|
||||||
from mcp_python.transport import SSETransport
|
from mcp_python.transport import SSETransport
|
||||||
|
|
||||||
|
|
@ -327,7 +327,7 @@ async def message_endpoint(request: Request):
|
||||||
return await transport.handle_message(request)
|
return await transport.handle_message(request)
|
||||||
```
|
```
|
||||||
|
|
||||||
### WebSocket Transport
|
### WebSocket transport
|
||||||
```python
|
```python
|
||||||
from mcp_python.transport import WebSocketTransport
|
from mcp_python.transport import WebSocketTransport
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
---
|
---
|
||||||
title: "Python Type Hints"
|
title: "Python type hints"
|
||||||
description: "Python type system documentation"
|
description: "Python type system documentation"
|
||||||
---
|
---
|
||||||
|
|
||||||
## Core Types
|
## Core types
|
||||||
|
|
||||||
### Resource Types
|
### Resource types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from typing import TypedDict, Optional, Literal, Union, List
|
from typing import TypedDict, Optional, Literal, Union, List
|
||||||
|
|
@ -38,7 +38,7 @@ class BlobResourceContents(ResourceContents):
|
||||||
blob: bytes # Base64 encoded
|
blob: bytes # Base64 encoded
|
||||||
```
|
```
|
||||||
|
|
||||||
### Message Types
|
### Message types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class TextContent(TypedDict):
|
class TextContent(TypedDict):
|
||||||
|
|
@ -60,7 +60,7 @@ class Message(TypedDict):
|
||||||
content: MessageContent
|
content: MessageContent
|
||||||
```
|
```
|
||||||
|
|
||||||
### Tool Types
|
### Tool types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ToolParameter(TypedDict):
|
class ToolParameter(TypedDict):
|
||||||
|
|
@ -82,7 +82,7 @@ class Tool(TypedDict):
|
||||||
inputSchema: ToolSchema
|
inputSchema: ToolSchema
|
||||||
```
|
```
|
||||||
|
|
||||||
### Prompt Types
|
### Prompt types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class PromptArgument(TypedDict):
|
class PromptArgument(TypedDict):
|
||||||
|
|
@ -103,7 +103,7 @@ class PromptResponse(TypedDict):
|
||||||
messages: List[Message]
|
messages: List[Message]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Progress Types
|
### Progress types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Progress(TypedDict):
|
class Progress(TypedDict):
|
||||||
|
|
@ -112,7 +112,7 @@ class Progress(TypedDict):
|
||||||
total: Optional[int]
|
total: Optional[int]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Logging Types
|
### Logging types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
LogLevel = Literal["debug", "info", "warning", "error"]
|
LogLevel = Literal["debug", "info", "warning", "error"]
|
||||||
|
|
@ -124,9 +124,9 @@ class LogMessage(TypedDict):
|
||||||
data: Any # JSON-serializable value
|
data: Any # JSON-serializable value
|
||||||
```
|
```
|
||||||
|
|
||||||
## Request/Response Types
|
## Request/Response types
|
||||||
|
|
||||||
### Resource Requests
|
### Resource requests
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ListResourcesRequest(TypedDict):
|
class ListResourcesRequest(TypedDict):
|
||||||
|
|
@ -155,7 +155,7 @@ class UnsubscribeRequest(TypedDict):
|
||||||
uri: str
|
uri: str
|
||||||
```
|
```
|
||||||
|
|
||||||
### Tool Requests
|
### Tool requests
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ListToolsRequest(TypedDict):
|
class ListToolsRequest(TypedDict):
|
||||||
|
|
@ -177,7 +177,7 @@ class CallToolResponse(TypedDict):
|
||||||
toolResult: Any # JSON-serializable value
|
toolResult: Any # JSON-serializable value
|
||||||
```
|
```
|
||||||
|
|
||||||
### Prompt Requests
|
### Prompt requests
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ListPromptsRequest(TypedDict):
|
class ListPromptsRequest(TypedDict):
|
||||||
|
|
@ -195,7 +195,7 @@ class GetPromptRequest(TypedDict):
|
||||||
arguments: Optional[dict[str, str]]
|
arguments: Optional[dict[str, str]]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Protocol Types
|
## Protocol types
|
||||||
|
|
||||||
### Implementation Info
|
### Implementation Info
|
||||||
|
|
||||||
|
|
@ -231,9 +231,9 @@ class ServerCapabilities(TypedDict):
|
||||||
tools: Optional[ToolCapabilities]
|
tools: Optional[ToolCapabilities]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Transport Types
|
## Transport types
|
||||||
|
|
||||||
### Stream Types
|
### Stream types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ReadStream(Protocol):
|
class ReadStream(Protocol):
|
||||||
|
|
@ -245,7 +245,7 @@ class WriteStream(Protocol):
|
||||||
async def write(self, data: bytes) -> None: ...
|
async def write(self, data: bytes) -> None: ...
|
||||||
```
|
```
|
||||||
|
|
||||||
### Session Types
|
### Session types
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ServerSession(Protocol):
|
class ServerSession(Protocol):
|
||||||
|
|
@ -259,9 +259,9 @@ class ServerSession(Protocol):
|
||||||
) -> None: ...
|
) -> None: ...
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage Examples
|
## Usage examples
|
||||||
|
|
||||||
### Type Usage in Handlers
|
### Type usage in handlers
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from mcp_python.server import Server
|
from mcp_python.server import Server
|
||||||
|
|
@ -314,7 +314,7 @@ async def get_prompt(name: str, arguments: dict) -> PromptResponse:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Type Checking
|
### Type checking
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from typing import TypeGuard
|
from typing import TypeGuard
|
||||||
|
|
@ -330,7 +330,7 @@ def process_message(message: Message) -> str:
|
||||||
return f"Image message with type: {content['mimeType']}"
|
return f"Image message with type: {content['mimeType']}"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Additional Notes
|
## Additional notes
|
||||||
|
|
||||||
- All types are compatible with standard Python type checkers (mypy, pyright)
|
- All types are compatible with standard Python type checkers (mypy, pyright)
|
||||||
- TypedDict is used extensively to ensure proper structure of JSON-like objects
|
- TypedDict is used extensively to ensure proper structure of JSON-like objects
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
---
|
---
|
||||||
title: "Event Handling"
|
title: "Event handling"
|
||||||
description: "Handle events and manage state in your MCP server"
|
description: "Handle events and manage state in your MCP server"
|
||||||
---
|
---
|
||||||
|
|
||||||
MCP servers process various events including requests, notifications, and connection lifecycle events. This guide explains how to implement event handlers effectively in your server.
|
MCP servers process various events including requests, notifications, and connection lifecycle events. This guide explains how to implement event handlers effectively in your server.
|
||||||
|
|
||||||
## Request handlers
|
## Request handling
|
||||||
|
|
||||||
Request handlers process incoming requests from clients and must return a response. Each handler is associated with a specific method name.
|
Request handlers process incoming requests from clients and must return a response. Each handler is associated with a specific method name.
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ async def handle_list_tools(request: ListToolsRequest) -> ListToolsResult:
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
|
|
||||||
## Notification handlers
|
## Notification handling
|
||||||
|
|
||||||
Notification handlers process one-way messages that don't require responses. They're useful for handling events like resource updates or logging messages.
|
Notification handlers process one-way messages that don't require responses. They're useful for handling events like resource updates or logging messages.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Resource Management"
|
title: "Resource management"
|
||||||
description: "How to implement resource management in your MCP server"
|
description: "How to implement resource management in your MCP server"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Tool Implementation"
|
title: "Tool implementation"
|
||||||
description: "Learn how to implement tools in your MCP server"
|
description: "Learn how to implement tools in your MCP server"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
---
|
---
|
||||||
title: "TypeScript Class Reference"
|
title: "TypeScript class reference"
|
||||||
description: "Complete TypeScript SDK class documentation"
|
description: "Complete TypeScript SDK class documentation"
|
||||||
---
|
---
|
||||||
|
|
||||||
## Client Classes
|
## Client classes
|
||||||
|
|
||||||
### Client
|
### Client
|
||||||
|
|
||||||
|
|
@ -65,7 +65,7 @@ getServerVersion(): Implementation | undefined
|
||||||
```
|
```
|
||||||
Returns the server's version information after connection.
|
Returns the server's version information after connection.
|
||||||
|
|
||||||
### Transport Classes
|
### Transport classes
|
||||||
|
|
||||||
#### StdioClientTransport
|
#### StdioClientTransport
|
||||||
```typescript
|
```typescript
|
||||||
|
|
@ -103,7 +103,7 @@ import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/webso
|
||||||
constructor(url: URL)
|
constructor(url: URL)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Server Classes
|
## Server classes
|
||||||
|
|
||||||
### Server
|
### Server
|
||||||
|
|
||||||
|
|
@ -173,7 +173,7 @@ getClientVersion(): Implementation | undefined
|
||||||
```
|
```
|
||||||
Returns the client's version information after connection.
|
Returns the client's version information after connection.
|
||||||
|
|
||||||
### Transport Classes
|
### Transport classes
|
||||||
|
|
||||||
#### StdioServerTransport
|
#### StdioServerTransport
|
||||||
```typescript
|
```typescript
|
||||||
|
|
@ -197,7 +197,7 @@ constructor(endpoint: string, res: ServerResponse)
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Client Implementation
|
### Client implementation
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Client } from "@modelcontextprotocol/sdk/client";
|
import { Client } from "@modelcontextprotocol/sdk/client";
|
||||||
|
|
@ -269,7 +269,7 @@ async function main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Server Implementation
|
### Server implementation
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Server } from "@modelcontextprotocol/sdk/server";
|
import { Server } from "@modelcontextprotocol/sdk/server";
|
||||||
|
|
@ -363,7 +363,7 @@ async function main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Best Practices
|
## Best practices
|
||||||
|
|
||||||
1. **Error Handling**
|
1. **Error Handling**
|
||||||
```typescript
|
```typescript
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
---
|
---
|
||||||
title: "TypeScript Type Definitions"
|
title: "TypeScript type definitions"
|
||||||
description: "TypeScript type system documentation"
|
description: "TypeScript type system documentation"
|
||||||
---
|
---
|
||||||
|
|
||||||
## Core Protocol Types
|
## Core protocol types
|
||||||
|
|
||||||
### JSON-RPC Types
|
### JSON-RPC types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export const JSONRPC_VERSION = "2.0";
|
export const JSONRPC_VERSION = "2.0";
|
||||||
|
|
@ -53,7 +53,7 @@ export type JSONRPCMessage =
|
||||||
| JSONRPCError;
|
| JSONRPCError;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Message Content Types
|
### Message content types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
/** Text provided to or from an LLM */
|
/** Text provided to or from an LLM */
|
||||||
|
|
@ -84,7 +84,7 @@ export interface Progress {
|
||||||
export type ProgressCallback = (progress: Progress) => void;
|
export type ProgressCallback = (progress: Progress) => void;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Resource Types
|
### Resource types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
/** The contents of a specific resource */
|
/** The contents of a specific resource */
|
||||||
|
|
@ -120,7 +120,7 @@ export interface ResourceTemplate {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Tool Types
|
### Tool types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
/** Definition for a tool the client can call */
|
/** Definition for a tool the client can call */
|
||||||
|
|
@ -134,7 +134,7 @@ export interface Tool {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Prompt Types
|
### Prompt types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
/** Describes an argument that a prompt can accept */
|
/** Describes an argument that a prompt can accept */
|
||||||
|
|
@ -152,9 +152,9 @@ export interface Prompt {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Request/Response Types
|
## Request/response types
|
||||||
|
|
||||||
### Resource Requests
|
### Resource requests
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface ListResourcesRequest {
|
export interface ListResourcesRequest {
|
||||||
|
|
@ -195,7 +195,7 @@ export interface UnsubscribeRequest {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Tool Requests
|
### Tool requests
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface ListToolsRequest {
|
export interface ListToolsRequest {
|
||||||
|
|
@ -226,7 +226,7 @@ export interface CallToolResult {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Prompt Requests
|
### Prompt requests
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface ListPromptsRequest {
|
export interface ListPromptsRequest {
|
||||||
|
|
@ -255,9 +255,9 @@ export interface GetPromptResult {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Protocol Types
|
## Protocol types
|
||||||
|
|
||||||
### Implementation Info
|
### Implementation info
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface Implementation {
|
export interface Implementation {
|
||||||
|
|
@ -266,7 +266,7 @@ export interface Implementation {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Capability Types
|
### Capability types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface ResourceCapabilities {
|
export interface ResourceCapabilities {
|
||||||
|
|
@ -296,7 +296,7 @@ export interface ClientCapabilities {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Notification Types
|
### Notification types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface ResourceUpdatedNotification {
|
export interface ResourceUpdatedNotification {
|
||||||
|
|
@ -328,7 +328,7 @@ export type ServerNotification =
|
||||||
| LogMessageNotification;
|
| LogMessageNotification;
|
||||||
```
|
```
|
||||||
|
|
||||||
## Error Types
|
## Error types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export enum ErrorCode {
|
export enum ErrorCode {
|
||||||
|
|
@ -354,7 +354,7 @@ export class McpError extends Error {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Transport Types
|
## Transport types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
export interface Transport {
|
export interface Transport {
|
||||||
|
|
@ -373,9 +373,9 @@ export interface StdioServerParameters {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage Examples
|
## Usage examples
|
||||||
|
|
||||||
### Type Guards
|
### Type guards
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function isTextContent(content:
|
function isTextContent(content:
|
||||||
|
|
@ -391,7 +391,7 @@ function isResourceUpdatedNotification(
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Type Assertions
|
### Type assertions
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function assertResource(data: unknown): asserts data is Resource {
|
function assertResource(data: unknown): asserts data is Resource {
|
||||||
|
|
@ -409,7 +409,7 @@ function assertResource(data: unknown): asserts data is Resource {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using Generics
|
### Using generics
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
async function handleResourceContent<T extends ResourceContents>(
|
async function handleResourceContent<T extends ResourceContents>(
|
||||||
|
|
@ -425,7 +425,7 @@ async function handleResourceContent<T extends ResourceContents>(
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Working with Union Types
|
### Working with union types
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function processMessage(message: SamplingMessage): string {
|
function processMessage(message: SamplingMessage): string {
|
||||||
|
|
@ -442,7 +442,7 @@ function processMessage(message: SamplingMessage): string {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Type Utilities
|
## Type utilities
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
/** Helper type to extract request params from method name */
|
/** Helper type to extract request params from method name */
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
title: "Core Architecture"
|
title: "Core architecture"
|
||||||
description: "Understand how MCP connects clients, servers, and LLMs"
|
description: "Understand how MCP connects clients, servers, and LLMs"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -27,7 +27,7 @@ flowchart LR
|
||||||
|
|
||||||
## 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. Key classes include:
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ class Protocol<SendRequestT, SendNotificationT, SendResultT> {
|
||||||
|
|
||||||
The transport layer handles the actual communication between clients and servers. MCP supports multiple transport mechanisms:
|
The transport layer handles the actual communication between clients and servers. MCP supports multiple transport mechanisms:
|
||||||
|
|
||||||
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
|
```typescript
|
||||||
|
|
@ -61,7 +61,7 @@ The transport layer handles the actual communication between clients and servers
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. **WebSocket Transport**
|
2. **WebSocket transport**
|
||||||
- Enables real-time bidirectional communication
|
- Enables real-time bidirectional communication
|
||||||
- Supports remote connections
|
- Supports remote connections
|
||||||
```typescript
|
```typescript
|
||||||
|
|
@ -71,7 +71,7 @@ The transport layer handles the actual communication between clients and servers
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3. **SSE Transport**
|
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
|
```typescript
|
||||||
|
|
@ -136,7 +136,7 @@ sequenceDiagram
|
||||||
3. Client sends `initialized` notification
|
3. Client sends `initialized` notification
|
||||||
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:
|
||||||
|
|
||||||
|
|
@ -206,54 +206,54 @@ await server.connect(transport);
|
||||||
|
|
||||||
### Transport selection
|
### Transport selection
|
||||||
|
|
||||||
1. **Local Communication**
|
1. **Local communication**
|
||||||
- Use stdio transport for local processes
|
- Use stdio transport for local processes
|
||||||
- Efficient for same-machine communication
|
- Efficient for same-machine communication
|
||||||
- Simple process management
|
- Simple process management
|
||||||
|
|
||||||
2. **Remote Communication**
|
2. **Remote communication**
|
||||||
- WebSocket for full-duplex communication
|
- WebSocket for full-duplex communication
|
||||||
- SSE for scenarios requiring HTTP compatibility
|
- SSE for scenarios requiring HTTP compatibility
|
||||||
- Consider security implications
|
- Consider security implications
|
||||||
|
|
||||||
### Message Handling
|
### Message handling
|
||||||
|
|
||||||
1. **Request Processing**
|
1. **Request processing**
|
||||||
- Validate inputs thoroughly
|
- Validate inputs thoroughly
|
||||||
- Use type-safe schemas
|
- Use type-safe schemas
|
||||||
- Handle errors gracefully
|
- Handle errors gracefully
|
||||||
- Implement timeouts
|
- Implement timeouts
|
||||||
|
|
||||||
2. **Progress Reporting**
|
2. **Progress reporting**
|
||||||
- Use progress tokens for long operations
|
- Use progress tokens for long operations
|
||||||
- Report progress incrementally
|
- Report progress incrementally
|
||||||
- Include total progress when known
|
- Include total progress when known
|
||||||
|
|
||||||
3. **Error Management**
|
3. **Error management**
|
||||||
- Use appropriate error codes
|
- Use appropriate error codes
|
||||||
- Include helpful error messages
|
- Include helpful error messages
|
||||||
- Clean up resources on errors
|
- Clean up resources on errors
|
||||||
|
|
||||||
## Security considerations
|
## Security considerations
|
||||||
|
|
||||||
1. **Transport Security**
|
1. **Transport security**
|
||||||
- Use TLS for remote connections
|
- Use TLS for remote connections
|
||||||
- Validate connection origins
|
- Validate connection origins
|
||||||
- Implement authentication when needed
|
- Implement authentication when needed
|
||||||
|
|
||||||
2. **Message Validation**
|
2. **Message validation**
|
||||||
- Validate all incoming messages
|
- Validate all incoming messages
|
||||||
- Sanitize inputs
|
- Sanitize inputs
|
||||||
- Check message size limits
|
- Check message size limits
|
||||||
- Verify JSON-RPC format
|
- Verify JSON-RPC format
|
||||||
|
|
||||||
3. **Resource Protection**
|
3. **Resource protection**
|
||||||
- Implement access controls
|
- Implement access controls
|
||||||
- Validate resource paths
|
- Validate resource paths
|
||||||
- Monitor resource usage
|
- Monitor resource usage
|
||||||
- Rate limit requests
|
- Rate limit requests
|
||||||
|
|
||||||
4. **Error Handling**
|
4. **Error handling**
|
||||||
- Don't leak sensitive information
|
- Don't leak sensitive information
|
||||||
- Log security-relevant errors
|
- Log security-relevant errors
|
||||||
- Implement proper cleanup
|
- Implement proper cleanup
|
||||||
|
|
@ -277,6 +277,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
|
||||||
|
|
||||||
This architecture provides a robust foundation for building MCP integrations while maintaining flexibility for different use cases and deployment scenarios.
|
|
||||||
|
|
@ -97,7 +97,7 @@ To use a prompt, clients make a `prompts/get` request:
|
||||||
|
|
||||||
Prompts can be dynamic and include:
|
Prompts can be dynamic and include:
|
||||||
|
|
||||||
### Context Integration
|
### Context integration
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const analyzeLogsPrompt = {
|
const analyzeLogsPrompt = {
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ The protocol and path structure is defined by the MCP server implementation. Ser
|
||||||
|
|
||||||
Resources can contain two types of content:
|
Resources can contain two types of content:
|
||||||
|
|
||||||
### Text Resources
|
### Text resources
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ When implementing sampling:
|
||||||
9. Test with various model parameters
|
9. Test with various model parameters
|
||||||
10. Monitor sampling costs
|
10. Monitor sampling costs
|
||||||
|
|
||||||
## Human-in-the-loop controls
|
## Human in the loop controls
|
||||||
|
|
||||||
Sampling is designed with human oversight in mind:
|
Sampling is designed with human oversight in mind:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,6 @@ Each tool is defined with the following structure:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `inputSchema` uses JSON Schema to define the expected parameters and their types. This enables:
|
|
||||||
|
|
||||||
- Parameter validation
|
|
||||||
- Type checking
|
|
||||||
- Documentation of required vs optional parameters
|
|
||||||
- Complex nested parameter structures
|
|
||||||
|
|
||||||
## Implementing tools
|
## Implementing tools
|
||||||
|
|
||||||
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:
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ const transport = new WebSocketClientTransport(
|
||||||
await client.connect(transport);
|
await client.connect(transport);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Server-sent events (SSE)
|
### 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.
|
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.
|
||||||
|
|
||||||
|
|
@ -256,21 +256,21 @@ Tips for debugging transport issues:
|
||||||
|
|
||||||
Choose the appropriate transport based on your needs:
|
Choose the appropriate transport based on your needs:
|
||||||
|
|
||||||
### Use stdio when:
|
### Use stdio when
|
||||||
|
|
||||||
- Building command-line tools
|
- Building command-line tools
|
||||||
- Implementing local integrations
|
- Implementing local integrations
|
||||||
- Needing simple process communication
|
- Needing simple process communication
|
||||||
- Working with shell scripts
|
- Working with shell scripts
|
||||||
|
|
||||||
### Use WebSocket when:
|
### Use WebSocket when
|
||||||
|
|
||||||
- Real-time updates are needed
|
- Real-time updates are needed
|
||||||
- Low latency is required
|
- Low latency is required
|
||||||
- Bidirectional communication is necessary
|
- Bidirectional communication is necessary
|
||||||
- Working with web applications
|
- Working with web applications
|
||||||
|
|
||||||
### Use SSE when:
|
### Use SSE when
|
||||||
|
|
||||||
- Only server-to-client streaming is needed
|
- Only server-to-client streaming is needed
|
||||||
- WebSocket support is limited
|
- WebSocket support is limited
|
||||||
|
|
|
||||||
|
|
@ -427,7 +427,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
|
||||||
</Card>
|
</Card>
|
||||||
</CardGroup>
|
</CardGroup>
|
||||||
|
|
||||||
## Available Transports
|
## Available transports
|
||||||
|
|
||||||
While this guide uses stdio transport, MCP supports multiple transport options:
|
While this guide uses stdio transport, MCP supports multiple transport options:
|
||||||
|
|
||||||
|
|
@ -445,7 +445,7 @@ transport = SSEServerTransport("/events", response)
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
### Installation Issues
|
### Installation issues
|
||||||
```bash
|
```bash
|
||||||
# Check Python version
|
# Check Python version
|
||||||
python --version
|
python --version
|
||||||
|
|
@ -455,14 +455,14 @@ pip uninstall -r requirements.txt
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
```
|
```
|
||||||
|
|
||||||
### Runtime Errors
|
### Runtime errors
|
||||||
```bash
|
```bash
|
||||||
# Enable debug logging
|
# Enable debug logging
|
||||||
export MCP_LOG_LEVEL=DEBUG
|
export MCP_LOG_LEVEL=DEBUG
|
||||||
python weather_server.py
|
python weather_server.py
|
||||||
```
|
```
|
||||||
|
|
||||||
### Type Checking
|
### Type checking
|
||||||
```bash
|
```bash
|
||||||
# Install mypy
|
# Install mypy
|
||||||
pip install mypy
|
pip install mypy
|
||||||
|
|
@ -523,7 +523,7 @@ mypy weather_server.py
|
||||||
</Accordion>
|
</Accordion>
|
||||||
</AccordionGroup>
|
</AccordionGroup>
|
||||||
|
|
||||||
## Advanced Features
|
## Advanced features
|
||||||
|
|
||||||
<Steps>
|
<Steps>
|
||||||
<Step title="Add caching">
|
<Step title="Add caching">
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue