This commit is contained in:
Zack Lee 2024-11-20 12:24:08 -05:00
commit ba7d5b866d
No known key found for this signature in database
2 changed files with 79 additions and 1432 deletions

View file

@ -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

View file

@ -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