Add Python stubs
This commit is contained in:
parent
61f6e3483d
commit
a2e6868c40
1 changed files with 65 additions and 47 deletions
|
|
@ -25,7 +25,7 @@ flowchart LR
|
||||||
subgraph "Server Process"
|
subgraph "Server Process"
|
||||||
server2[MCP Server]
|
server2[MCP Server]
|
||||||
end
|
end
|
||||||
|
|
||||||
client1 <-->|Transport Layer| server1
|
client1 <-->|Transport Layer| server1
|
||||||
client2 <-->|Transport Layer| server2
|
client2 <-->|Transport Layer| server2
|
||||||
```
|
```
|
||||||
|
|
@ -34,23 +34,32 @@ flowchart LR
|
||||||
|
|
||||||
### Protocol layer
|
### Protocol layer
|
||||||
|
|
||||||
The protocol layer handles message framing, request/response linking, and high-level communication patterns.
|
The protocol layer handles message framing, request/response linking, and high-level communication patterns.
|
||||||
|
|
||||||
```typescript
|
<Tabs>
|
||||||
class Protocol<Request, Notification, Result> {
|
<Tab title="TypeScript">
|
||||||
// Handle incoming requests
|
```typescript
|
||||||
setRequestHandler<T>(schema: T, handler: (request: T, extra: RequestHandlerExtra) => Promise<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) => Promise<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, options?: RequestOptions): 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:
|
Key classes include:
|
||||||
|
|
||||||
|
|
@ -116,11 +125,11 @@ MCP has these main types of messages:
|
||||||
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
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -169,35 +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/index.js";
|
<Tab title="TypeScript">
|
||||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
```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: {
|
capabilities: {
|
||||||
resources: {}
|
resources: {}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle requests
|
|
||||||
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
||||||
return {
|
|
||||||
resources: [
|
|
||||||
{
|
|
||||||
uri: "example://resource",
|
|
||||||
name: "Example Resource"
|
|
||||||
}
|
}
|
||||||
]
|
});
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Connect transport
|
// Handle requests
|
||||||
const transport = new StdioServerTransport();
|
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
||||||
await server.connect(transport);
|
return {
|
||||||
```
|
resources: [
|
||||||
|
{
|
||||||
|
uri: "example://resource",
|
||||||
|
name: "Example Resource"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Connect transport
|
||||||
|
const transport = new StdioServerTransport();
|
||||||
|
await server.connect(transport);
|
||||||
|
```
|
||||||
|
</Tab>
|
||||||
|
<Tab title="Python">
|
||||||
|
```python
|
||||||
|
# Python implementation coming soon
|
||||||
|
```
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
## Best practices
|
## Best practices
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue