Skip to content

Commit 7029b00

Browse files
authored
New examples docs (full-ingest style) (#6)
* new example docs * fixed nav * fix doc index and TOC * examples feature matrix * populate transport column * link to example headings
1 parent 9208880 commit 7029b00

35 files changed

+1014
-16488
lines changed

docs/asgi-integration.md

Lines changed: 0 additions & 762 deletions
This file was deleted.

docs/authentication.md

Lines changed: 0 additions & 596 deletions
This file was deleted.

docs/completions.md

Lines changed: 0 additions & 1053 deletions
This file was deleted.

docs/context.md

Lines changed: 0 additions & 654 deletions
This file was deleted.

docs/display-utilities.md

Lines changed: 0 additions & 1287 deletions
This file was deleted.

docs/elicitation.md

Lines changed: 0 additions & 442 deletions
This file was deleted.

docs/examples-authentication.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Authentication examples
2+
3+
MCP supports OAuth 2.1 authentication for protecting server resources. This section demonstrates both server-side token verification and client-side authentication flows.
4+
5+
## OAuth server implementation
6+
7+
FastMCP server with OAuth token verification:
8+
9+
```python
10+
--8<-- "examples/snippets/servers/oauth_server.py"
11+
```
12+
13+
This example shows:
14+
15+
- Implementing the `TokenVerifier` protocol for token validation
16+
- Using `AuthSettings` for RFC 9728 Protected Resource Metadata
17+
- Resource server configuration with authorization server discovery
18+
- Protected tools that require authentication
19+
20+
## Complete authentication server
21+
22+
Full Authorization Server implementation with token introspection:
23+
24+
```python
25+
--8<-- "examples/servers/simple-auth/mcp_simple_auth/auth_server.py"
26+
```
27+
28+
This comprehensive example includes:
29+
30+
- OAuth 2.1 authorization flows (authorization code, refresh token)
31+
- Token introspection endpoint for resource servers
32+
- Client registration and metadata management
33+
- RFC 9728 protected resource metadata endpoint
34+
35+
## Resource server with introspection
36+
37+
MCP Resource Server that validates tokens via Authorization Server introspection:
38+
39+
```python
40+
--8<-- "examples/servers/simple-auth/mcp_simple_auth/server.py"
41+
```
42+
43+
This demonstrates:
44+
45+
- Token introspection for validation instead of local token verification
46+
- Separation of Authorization Server (AS) and Resource Server (RS)
47+
- Protected MCP tools and resources
48+
- Production-ready server patterns
49+
50+
## Token verification implementation
51+
52+
Custom token verification logic:
53+
54+
```python
55+
--8<-- "examples/servers/simple-auth/mcp_simple_auth/token_verifier.py"
56+
```
57+
58+
This component handles:
59+
60+
- HTTP token introspection requests
61+
- Token validation with scope checking
62+
- RFC 8707 resource parameter validation
63+
- Error handling and logging
64+
65+
## Simple authentication provider
66+
67+
Authentication provider for development and testing:
68+
69+
```python
70+
--8<-- "examples/servers/simple-auth/mcp_simple_auth/simple_auth_provider.py"
71+
```
72+
73+
This utility provides:
74+
75+
- Simplified token generation for testing
76+
- Development authentication flows
77+
- Testing utilities for protected resources
78+
79+
## Legacy Authorization Server
80+
81+
Backward compatibility with older OAuth implementations:
82+
83+
```python
84+
--8<-- "examples/servers/simple-auth/mcp_simple_auth/legacy_as_server.py"
85+
```
86+
87+
This example shows:
88+
89+
- Support for non-RFC 9728 compliant clients
90+
- Legacy endpoint compatibility
91+
- Migration patterns for existing systems
92+
93+
## OAuth architecture
94+
95+
The MCP OAuth implementation follows the OAuth 2.1 authorization code flow with token introspection:
96+
97+
```mermaid
98+
sequenceDiagram
99+
participant C as Client
100+
participant AS as Authorization Server
101+
participant RS as Resource Server<br/>(MCP Server)
102+
participant U as User
103+
104+
Note over C,RS: 1. Discovery Phase (RFC 9728)
105+
C->>RS: GET /.well-known/oauth-protected-resource
106+
RS->>C: Protected Resource Metadata<br/>(issuer, scopes, etc.)
107+
108+
Note over C,AS: 2. Authorization Phase
109+
C->>AS: GET /authorize?response_type=code&client_id=...
110+
AS->>U: Redirect to login/consent
111+
U->>AS: User authenticates and consents
112+
AS->>C: Authorization code (via redirect)
113+
114+
Note over C,AS: 3. Token Exchange
115+
C->>AS: POST /token<br/>(authorization_code grant)
116+
AS->>C: Access token + refresh token
117+
118+
Note over C,RS: 4. Resource Access
119+
C->>RS: MCP request + Authorization: Bearer <token>
120+
RS->>AS: POST /introspect<br/>(validate token)
121+
AS->>RS: Token info (active, scopes, user)
122+
RS->>C: MCP response (if authorized)
123+
124+
Note over C,AS: 5. Token Refresh (when needed)
125+
C->>AS: POST /token<br/>(refresh_token grant)
126+
AS->>C: New access token
127+
```
128+
129+
**Components:**
130+
131+
- **Authorization Server (AS)**: Handles OAuth flows, issues and validates tokens
132+
- **Resource Server (RS)**: Your MCP server that validates tokens and serves protected resources
133+
- **Client**: Discovers AS through RFC 9728, obtains tokens, and uses them with MCP server
134+
- **User**: Resource owner who authorizes access
135+
136+
## Security considerations
137+
138+
When implementing authentication:
139+
140+
1. **Use HTTPS**: All OAuth flows must use HTTPS in production
141+
2. **Token validation**: Always validate tokens on the resource server side
142+
3. **Scope checking**: Verify that tokens have required scopes
143+
4. **Introspection**: Use token introspection for distributed validation
144+
5. **RFC compliance**: Follow RFC 9728 for proper AS discovery
145+
146+
These examples provide a complete OAuth 2.1 implementation suitable for production use with proper security practices.

docs/examples-clients.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Client examples
2+
3+
MCP clients connect to servers to access tools, resources, and prompts. This section demonstrates various client patterns and connection types.
4+
5+
## Basic stdio client
6+
7+
Connecting to MCP servers over stdio transport:
8+
9+
```python
10+
--8<-- "examples/snippets/clients/stdio_client.py"
11+
```
12+
13+
This fundamental example demonstrates:
14+
15+
- Creating `StdioServerParameters` for server connection
16+
- Using `ClientSession` for MCP communication
17+
- Listing and calling tools, reading resources, getting prompts
18+
- Handling both structured and unstructured tool results
19+
- Sampling callback implementation for LLM integration
20+
21+
## Streamable HTTP client
22+
23+
Connecting to HTTP-based MCP servers:
24+
25+
```python
26+
--8<-- "examples/snippets/clients/streamable_basic.py"
27+
```
28+
29+
This example shows:
30+
31+
- Using `streamablehttp_client` for HTTP connections
32+
- Simpler connection setup for web-deployed servers
33+
- Basic tool listing and execution over HTTP
34+
35+
## Display utilities
36+
37+
Helper utilities for client user interfaces:
38+
39+
```python
40+
--8<-- "examples/snippets/clients/display_utilities.py"
41+
```
42+
43+
This practical example covers:
44+
45+
- Using `get_display_name()` for human-readable names
46+
- Proper precedence rules for tool/resource titles
47+
- Building user-friendly client interfaces
48+
- Consistent naming across different MCP objects
49+
50+
## OAuth authentication client
51+
52+
Client-side OAuth 2.1 authentication flow:
53+
54+
```python
55+
--8<-- "examples/snippets/clients/oauth_client.py"
56+
```
57+
58+
This comprehensive example demonstrates:
59+
60+
- `OAuthClientProvider` setup and configuration
61+
- Token storage with custom `TokenStorage` implementation
62+
- Authorization flow handling (redirect and callback)
63+
- Authenticated requests to protected MCP servers
64+
65+
## Completion client
66+
67+
Using completion suggestions for better user experience:
68+
69+
```python
70+
--8<-- "examples/snippets/clients/completion_client.py"
71+
```
72+
73+
This advanced example shows:
74+
75+
- Resource template argument completion
76+
- Context-aware completions (e.g., repository suggestions based on owner)
77+
- Prompt argument completion
78+
- Dynamic suggestion generation
79+
80+
## Tool result parsing
81+
82+
Understanding and processing tool results:
83+
84+
```python
85+
--8<-- "examples/snippets/clients/parsing_tool_results.py"
86+
```
87+
88+
This detailed example covers:
89+
90+
- Parsing different content types (`TextContent`, `ImageContent`, `EmbeddedResource`)
91+
- Handling structured output data
92+
- Processing embedded resources
93+
- Error handling for failed tool executions
94+
95+
## Complete chatbot client
96+
97+
A full-featured chatbot that integrates with multiple MCP servers:
98+
99+
```python
100+
--8<-- "examples/clients/simple-chatbot/mcp_simple_chatbot/main.py"
101+
```
102+
103+
This production-ready example includes:
104+
105+
- **Multi-server management**: Connect to multiple MCP servers simultaneously
106+
- **LLM integration**: Use Groq API for natural language processing
107+
- **Tool orchestration**: Automatic tool selection and execution
108+
- **Error handling**: Retry mechanisms and graceful failure handling
109+
- **Configuration management**: JSON-based server configuration
110+
- **Session management**: Persistent conversation context
111+
112+
## Authentication client
113+
114+
Complete OAuth client implementation:
115+
116+
```python
117+
--8<-- "examples/clients/simple-auth-client/mcp_simple_auth_client/main.py"
118+
```
119+
120+
This example demonstrates:
121+
122+
- Full OAuth 2.1 client implementation
123+
- Token management and refresh
124+
- Protected resource access
125+
- Integration with authenticated MCP servers
126+
127+
These examples provide comprehensive patterns for building MCP clients that can handle various server types, authentication methods, and interaction patterns.

docs/examples-echo-servers.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Echo server examples
2+
3+
Echo servers are simple examples that demonstrate basic MCP functionality by echoing input back to clients. These are useful for testing and understanding MCP fundamentals.
4+
5+
## Simple echo server
6+
7+
The most basic echo implementation:
8+
9+
```python
10+
--8<-- "examples/fastmcp/simple_echo.py"
11+
```
12+
13+
This minimal example shows:
14+
15+
- Single tool implementation with string input/output
16+
- Basic parameter handling
17+
- Simple string manipulation and return
18+
19+
## Enhanced echo server
20+
21+
More sophisticated echo patterns:
22+
23+
```python
24+
--8<-- "examples/fastmcp/echo.py"
25+
```
26+
27+
This enhanced version demonstrates:
28+
29+
- Multiple echo variants (basic echo, uppercase, reverse)
30+
- Different parameter types and patterns
31+
- Tool naming and description best practices
32+
33+
Echo servers are useful for:
34+
35+
- **Testing client connections**: Verify that your client can connect and call tools
36+
- **Understanding MCP basics**: Learn the fundamental request/response patterns
37+
- **Development and debugging**: Simple, predictable behavior for testing
38+
- **Protocol verification**: Ensure transport layers work correctly
39+
40+
## Usage patterns
41+
42+
These echo servers can be used to test different aspects of MCP:
43+
44+
```bash
45+
# Test with MCP Inspector
46+
uv run mcp dev echo.py
47+
48+
# Test direct execution
49+
python echo.py
50+
51+
# Test with custom clients
52+
# (Use the client examples to connect to these echo servers)
53+
```
54+
55+
## Testing tool calls
56+
57+
Example tool calls you can make to echo servers:
58+
59+
```json
60+
{
61+
"tool": "echo",
62+
"arguments": {
63+
"message": "Hello, MCP!"
64+
}
65+
}
66+
```
67+
68+
Expected response:
69+
```json
70+
{
71+
"result": "Echo: Hello, MCP!"
72+
}
73+
```
74+
75+
Echo servers provide a foundation for understanding MCP patterns before building more complex functionality.

0 commit comments

Comments
 (0)