grammar
This commit is contained in:
parent
6cac5a1cde
commit
ba7d5b866d
13 changed files with 79 additions and 1432 deletions
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: "Connection Management"
|
||||
title: "Connection management"
|
||||
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"
|
||||
---
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: "Tool Invocation"
|
||||
title: "Tool invocation"
|
||||
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'
|
||||
---
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: 'Message Types'
|
||||
title: 'Message types'
|
||||
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)"
|
||||
---
|
||||
|
||||
|
|
@ -22,10 +22,8 @@ The Model Context Protocol (MCP) is a standardized protocol that enables AI appl
|
|||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
# Core concepts
|
||||
|
||||
## Core concepts
|
||||
MCP defines several key primitives that form the foundation of the protocol:
|
||||
|
||||
<CardGroup cols={2}>
|
||||
<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.
|
||||
|
|
@ -41,12 +39,10 @@ MCP defines several key primitives that form the foundation of the protocol:
|
|||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
# Protocol flow
|
||||
|
||||
## Connection and initialization
|
||||
## Protocol flow
|
||||
|
||||
### Connection and initialization
|
||||
When a client connects to an MCP server, they go through the following initialization sequence:
|
||||
|
||||
<Steps>
|
||||
<Step title="Client connects">
|
||||
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.
|
||||
|
||||
## Transport options
|
||||
|
||||
### Transport options
|
||||
MCP supports multiple transport mechanisms for client-server communication:
|
||||
|
||||
<AccordionGroup>
|
||||
<Accordion title="Standard input/output (stdio)">
|
||||
- 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"
|
||||
---
|
||||
|
||||
## Server Class
|
||||
## Server class
|
||||
|
||||
The main class for implementing MCP servers in Python.
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ Creates a new MCP server instance.
|
|||
|
||||
### Decorators
|
||||
|
||||
#### Resource Handling
|
||||
#### Resource handling
|
||||
|
||||
##### @server.list_resources()
|
||||
```python
|
||||
|
|
@ -72,7 +72,7 @@ async def unsubscribe_resource(uri: str) -> None:
|
|||
**Parameters:**
|
||||
- `uri` (str): Resource URI to unsubscribe from
|
||||
|
||||
#### Tool Support
|
||||
#### Tool support
|
||||
|
||||
##### @server.list_tools()
|
||||
```python
|
||||
|
|
@ -99,7 +99,7 @@ async def call_tool(name: str, **kwargs) -> Any:
|
|||
|
||||
**Returns:** Tool execution result (any JSON-serializable value)
|
||||
|
||||
#### Prompt Management
|
||||
#### Prompt management
|
||||
|
||||
##### @server.list_prompts()
|
||||
```python
|
||||
|
|
@ -128,7 +128,7 @@ async def get_prompt(name: str, arguments: dict) -> PromptResponse:
|
|||
- `messages` (List[Message]): List of prompt messages
|
||||
- `description` (str, optional): Description of the prompt response
|
||||
|
||||
### Session Methods
|
||||
### Session methods
|
||||
|
||||
Methods available on the server session object:
|
||||
|
||||
|
|
@ -158,7 +158,7 @@ async def send_log_message(
|
|||
"""Sends log messages to clients."""
|
||||
```
|
||||
|
||||
## Data Classes
|
||||
## Data classes
|
||||
|
||||
### Resource
|
||||
```python
|
||||
|
|
@ -204,7 +204,7 @@ class PromptResponse:
|
|||
description: Optional[str] = None
|
||||
```
|
||||
|
||||
## Example Usage
|
||||
## Example usage
|
||||
|
||||
Here's a complete example demonstrating the main features:
|
||||
|
||||
|
|
@ -263,7 +263,7 @@ if __name__ == "__main__":
|
|||
anyio.run(main)
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
## Best practices
|
||||
|
||||
1. **Error Handling**: Always implement proper error handling in your server methods:
|
||||
```python
|
||||
|
|
@ -303,15 +303,15 @@ if __name__ == "__main__":
|
|||
await session.send_resource_updated("example://resource1")
|
||||
```
|
||||
|
||||
## Transport Options
|
||||
## Transport options
|
||||
|
||||
### stdio Transport
|
||||
### Stdio transport
|
||||
```python
|
||||
async with stdio_server() as (read_stream, write_stream):
|
||||
await server.run(read_stream, write_stream)
|
||||
```
|
||||
|
||||
### SSE Transport
|
||||
### SSE transport
|
||||
```python
|
||||
from mcp_python.transport import SSETransport
|
||||
|
||||
|
|
@ -327,7 +327,7 @@ async def message_endpoint(request: Request):
|
|||
return await transport.handle_message(request)
|
||||
```
|
||||
|
||||
### WebSocket Transport
|
||||
### WebSocket transport
|
||||
```python
|
||||
from mcp_python.transport import WebSocketTransport
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
---
|
||||
title: "Python Type Hints"
|
||||
title: "Python type hints"
|
||||
description: "Python type system documentation"
|
||||
---
|
||||
|
||||
## Core Types
|
||||
## Core types
|
||||
|
||||
### Resource Types
|
||||
### Resource types
|
||||
|
||||
```python
|
||||
from typing import TypedDict, Optional, Literal, Union, List
|
||||
|
|
@ -38,7 +38,7 @@ class BlobResourceContents(ResourceContents):
|
|||
blob: bytes # Base64 encoded
|
||||
```
|
||||
|
||||
### Message Types
|
||||
### Message types
|
||||
|
||||
```python
|
||||
class TextContent(TypedDict):
|
||||
|
|
@ -60,7 +60,7 @@ class Message(TypedDict):
|
|||
content: MessageContent
|
||||
```
|
||||
|
||||
### Tool Types
|
||||
### Tool types
|
||||
|
||||
```python
|
||||
class ToolParameter(TypedDict):
|
||||
|
|
@ -82,7 +82,7 @@ class Tool(TypedDict):
|
|||
inputSchema: ToolSchema
|
||||
```
|
||||
|
||||
### Prompt Types
|
||||
### Prompt types
|
||||
|
||||
```python
|
||||
class PromptArgument(TypedDict):
|
||||
|
|
@ -103,7 +103,7 @@ class PromptResponse(TypedDict):
|
|||
messages: List[Message]
|
||||
```
|
||||
|
||||
### Progress Types
|
||||
### Progress types
|
||||
|
||||
```python
|
||||
class Progress(TypedDict):
|
||||
|
|
@ -112,7 +112,7 @@ class Progress(TypedDict):
|
|||
total: Optional[int]
|
||||
```
|
||||
|
||||
### Logging Types
|
||||
### Logging types
|
||||
|
||||
```python
|
||||
LogLevel = Literal["debug", "info", "warning", "error"]
|
||||
|
|
@ -124,9 +124,9 @@ class LogMessage(TypedDict):
|
|||
data: Any # JSON-serializable value
|
||||
```
|
||||
|
||||
## Request/Response Types
|
||||
## Request/Response types
|
||||
|
||||
### Resource Requests
|
||||
### Resource requests
|
||||
|
||||
```python
|
||||
class ListResourcesRequest(TypedDict):
|
||||
|
|
@ -155,7 +155,7 @@ class UnsubscribeRequest(TypedDict):
|
|||
uri: str
|
||||
```
|
||||
|
||||
### Tool Requests
|
||||
### Tool requests
|
||||
|
||||
```python
|
||||
class ListToolsRequest(TypedDict):
|
||||
|
|
@ -177,7 +177,7 @@ class CallToolResponse(TypedDict):
|
|||
toolResult: Any # JSON-serializable value
|
||||
```
|
||||
|
||||
### Prompt Requests
|
||||
### Prompt requests
|
||||
|
||||
```python
|
||||
class ListPromptsRequest(TypedDict):
|
||||
|
|
@ -195,7 +195,7 @@ class GetPromptRequest(TypedDict):
|
|||
arguments: Optional[dict[str, str]]
|
||||
```
|
||||
|
||||
## Protocol Types
|
||||
## Protocol types
|
||||
|
||||
### Implementation Info
|
||||
|
||||
|
|
@ -231,9 +231,9 @@ class ServerCapabilities(TypedDict):
|
|||
tools: Optional[ToolCapabilities]
|
||||
```
|
||||
|
||||
## Transport Types
|
||||
## Transport types
|
||||
|
||||
### Stream Types
|
||||
### Stream types
|
||||
|
||||
```python
|
||||
class ReadStream(Protocol):
|
||||
|
|
@ -245,7 +245,7 @@ class WriteStream(Protocol):
|
|||
async def write(self, data: bytes) -> None: ...
|
||||
```
|
||||
|
||||
### Session Types
|
||||
### Session types
|
||||
|
||||
```python
|
||||
class ServerSession(Protocol):
|
||||
|
|
@ -259,9 +259,9 @@ class ServerSession(Protocol):
|
|||
) -> None: ...
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
## Usage examples
|
||||
|
||||
### Type Usage in Handlers
|
||||
### Type usage in handlers
|
||||
|
||||
```python
|
||||
from mcp_python.server import Server
|
||||
|
|
@ -314,7 +314,7 @@ async def get_prompt(name: str, arguments: dict) -> PromptResponse:
|
|||
}
|
||||
```
|
||||
|
||||
### Type Checking
|
||||
### Type checking
|
||||
|
||||
```python
|
||||
from typing import TypeGuard
|
||||
|
|
@ -330,7 +330,7 @@ def process_message(message: Message) -> str:
|
|||
return f"Image message with type: {content['mimeType']}"
|
||||
```
|
||||
|
||||
## Additional Notes
|
||||
## Additional notes
|
||||
|
||||
- All types are compatible with standard Python type checkers (mypy, pyright)
|
||||
- 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"
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ async def handle_list_tools(request: ListToolsRequest) -> ListToolsResult:
|
|||
```
|
||||
</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.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: "Resource Management"
|
||||
title: "Resource management"
|
||||
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"
|
||||
---
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
---
|
||||
title: "TypeScript Class Reference"
|
||||
title: "TypeScript class reference"
|
||||
description: "Complete TypeScript SDK class documentation"
|
||||
---
|
||||
|
||||
## Client Classes
|
||||
## Client classes
|
||||
|
||||
### Client
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ getServerVersion(): Implementation | undefined
|
|||
```
|
||||
Returns the server's version information after connection.
|
||||
|
||||
### Transport Classes
|
||||
### Transport classes
|
||||
|
||||
#### StdioClientTransport
|
||||
```typescript
|
||||
|
|
@ -103,7 +103,7 @@ import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/webso
|
|||
constructor(url: URL)
|
||||
```
|
||||
|
||||
## Server Classes
|
||||
## Server classes
|
||||
|
||||
### Server
|
||||
|
||||
|
|
@ -173,7 +173,7 @@ getClientVersion(): Implementation | undefined
|
|||
```
|
||||
Returns the client's version information after connection.
|
||||
|
||||
### Transport Classes
|
||||
### Transport classes
|
||||
|
||||
#### StdioServerTransport
|
||||
```typescript
|
||||
|
|
@ -197,7 +197,7 @@ constructor(endpoint: string, res: ServerResponse)
|
|||
|
||||
## Examples
|
||||
|
||||
### Client Implementation
|
||||
### Client implementation
|
||||
|
||||
```typescript
|
||||
import { Client } from "@modelcontextprotocol/sdk/client";
|
||||
|
|
@ -269,7 +269,7 @@ async function main() {
|
|||
}
|
||||
```
|
||||
|
||||
### Server Implementation
|
||||
### Server implementation
|
||||
|
||||
```typescript
|
||||
import { Server } from "@modelcontextprotocol/sdk/server";
|
||||
|
|
@ -363,7 +363,7 @@ async function main() {
|
|||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
## Best practices
|
||||
|
||||
1. **Error Handling**
|
||||
```typescript
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
---
|
||||
title: "TypeScript Type Definitions"
|
||||
title: "TypeScript type definitions"
|
||||
description: "TypeScript type system documentation"
|
||||
---
|
||||
|
||||
## Core Protocol Types
|
||||
## Core protocol types
|
||||
|
||||
### JSON-RPC Types
|
||||
### JSON-RPC types
|
||||
|
||||
```typescript
|
||||
export const JSONRPC_VERSION = "2.0";
|
||||
|
|
@ -53,7 +53,7 @@ export type JSONRPCMessage =
|
|||
| JSONRPCError;
|
||||
```
|
||||
|
||||
### Message Content Types
|
||||
### Message content types
|
||||
|
||||
```typescript
|
||||
/** Text provided to or from an LLM */
|
||||
|
|
@ -84,7 +84,7 @@ export interface Progress {
|
|||
export type ProgressCallback = (progress: Progress) => void;
|
||||
```
|
||||
|
||||
### Resource Types
|
||||
### Resource types
|
||||
|
||||
```typescript
|
||||
/** The contents of a specific resource */
|
||||
|
|
@ -120,7 +120,7 @@ export interface ResourceTemplate {
|
|||
}
|
||||
```
|
||||
|
||||
### Tool Types
|
||||
### Tool types
|
||||
|
||||
```typescript
|
||||
/** Definition for a tool the client can call */
|
||||
|
|
@ -134,7 +134,7 @@ export interface Tool {
|
|||
}
|
||||
```
|
||||
|
||||
### Prompt Types
|
||||
### Prompt types
|
||||
|
||||
```typescript
|
||||
/** 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
|
||||
export interface ListResourcesRequest {
|
||||
|
|
@ -195,7 +195,7 @@ export interface UnsubscribeRequest {
|
|||
}
|
||||
```
|
||||
|
||||
### Tool Requests
|
||||
### Tool requests
|
||||
|
||||
```typescript
|
||||
export interface ListToolsRequest {
|
||||
|
|
@ -226,7 +226,7 @@ export interface CallToolResult {
|
|||
}
|
||||
```
|
||||
|
||||
### Prompt Requests
|
||||
### Prompt requests
|
||||
|
||||
```typescript
|
||||
export interface ListPromptsRequest {
|
||||
|
|
@ -255,9 +255,9 @@ export interface GetPromptResult {
|
|||
}
|
||||
```
|
||||
|
||||
## Protocol Types
|
||||
## Protocol types
|
||||
|
||||
### Implementation Info
|
||||
### Implementation info
|
||||
|
||||
```typescript
|
||||
export interface Implementation {
|
||||
|
|
@ -266,7 +266,7 @@ export interface Implementation {
|
|||
}
|
||||
```
|
||||
|
||||
### Capability Types
|
||||
### Capability types
|
||||
|
||||
```typescript
|
||||
export interface ResourceCapabilities {
|
||||
|
|
@ -296,7 +296,7 @@ export interface ClientCapabilities {
|
|||
}
|
||||
```
|
||||
|
||||
### Notification Types
|
||||
### Notification types
|
||||
|
||||
```typescript
|
||||
export interface ResourceUpdatedNotification {
|
||||
|
|
@ -328,7 +328,7 @@ export type ServerNotification =
|
|||
| LogMessageNotification;
|
||||
```
|
||||
|
||||
## Error Types
|
||||
## Error types
|
||||
|
||||
```typescript
|
||||
export enum ErrorCode {
|
||||
|
|
@ -354,7 +354,7 @@ export class McpError extends Error {
|
|||
}
|
||||
```
|
||||
|
||||
## Transport Types
|
||||
## Transport types
|
||||
|
||||
```typescript
|
||||
export interface Transport {
|
||||
|
|
@ -373,9 +373,9 @@ export interface StdioServerParameters {
|
|||
}
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
## Usage examples
|
||||
|
||||
### Type Guards
|
||||
### Type guards
|
||||
|
||||
```typescript
|
||||
function isTextContent(content:
|
||||
|
|
@ -391,7 +391,7 @@ function isResourceUpdatedNotification(
|
|||
}
|
||||
```
|
||||
|
||||
### Type Assertions
|
||||
### Type assertions
|
||||
|
||||
```typescript
|
||||
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
|
||||
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
|
||||
function processMessage(message: SamplingMessage): string {
|
||||
|
|
@ -442,7 +442,7 @@ function processMessage(message: SamplingMessage): string {
|
|||
}
|
||||
```
|
||||
|
||||
## Type Utilities
|
||||
## Type utilities
|
||||
|
||||
```typescript
|
||||
/** Helper type to extract request params from method name */
|
||||
|
|
|
|||
Loading…
Reference in a new issue