|
| 1 | +# MCP Client Examples |
| 2 | + |
| 3 | +This directory contains Model Context Protocol (MCP) client examples implemented in Rust. These examples demonstrate how to communicate with MCP servers using different transport methods and how to use various client APIs. |
| 4 | + |
| 5 | +## Example List |
| 6 | + |
| 7 | +### SSE Client (`sse.rs`) |
| 8 | + |
| 9 | +A client that communicates with an MCP server using Server-Sent Events (SSE) transport. |
| 10 | + |
| 11 | +- Connects to an MCP server running at `http://localhost:8000/sse` |
| 12 | +- Retrieves server information and list of available tools |
| 13 | +- Calls a tool named "increment" |
| 14 | + |
| 15 | +### Git Standard I/O Client (`git_stdio.rs`) |
| 16 | + |
| 17 | +A client that communicates with a Git-related MCP server using standard input/output. |
| 18 | + |
| 19 | +- Launches the `uvx mcp-server-git` command as a child process |
| 20 | +- Retrieves server information and list of available tools |
| 21 | +- Calls the `git_status` tool to check the Git status of the current directory |
| 22 | + |
| 23 | +### Streamable HTTP Client (`streamable_http.rs`) |
| 24 | + |
| 25 | +A client that communicates with an MCP server using HTTP streaming transport. |
| 26 | +- Connects to an MCP server running at `http://localhost:8000` |
| 27 | +- Retrieves server information and list of available tools |
| 28 | +- Calls a tool named "increment" |
| 29 | + |
| 30 | +### Full-Featured Standard I/O Client (`everything_stdio.rs`) |
| 31 | + |
| 32 | +An example demonstrating all MCP client capabilities. |
| 33 | + |
| 34 | +- Launches `npx -y @modelcontextprotocol/server-everything` as a child process |
| 35 | +- Retrieves server information and list of available tools |
| 36 | +- Calls various tools, including "echo" and "longRunningOperation" |
| 37 | +- Lists and reads available resources |
| 38 | +- Lists and retrieves simple and complex prompts |
| 39 | +- Lists available resource templates |
| 40 | + |
| 41 | +### Client Collection (`collection.rs`) |
| 42 | + |
| 43 | +An example showing how to manage multiple MCP clients. |
| 44 | + |
| 45 | +- Creates 10 clients connected to Git servers |
| 46 | +- Stores these clients in a HashMap |
| 47 | +- Performs the same sequence of operations on each client |
| 48 | +- Uses `into_dyn()` to convert services to dynamic services |
| 49 | + |
| 50 | +### OAuth Client (`auth/oauth_client.rs`) |
| 51 | + |
| 52 | +A client demonstrating how to authenticate with an MCP server using OAuth. |
| 53 | + |
| 54 | +- Starts a local HTTP server to handle OAuth callbacks |
| 55 | +- Initializes the OAuth state machine and begins the authorization flow |
| 56 | +- Displays the authorization URL and waits for user authorization |
| 57 | +- Establishes an authorized connection to the MCP server using the acquired access token |
| 58 | +- Demonstrates how to use the authorized connection to retrieve available tools and prompts |
| 59 | + |
| 60 | +## How to Run |
| 61 | + |
| 62 | +Each example can be run using Cargo: |
| 63 | + |
| 64 | +```bash |
| 65 | +# Run the SSE client example |
| 66 | +cargo run --example clients_sse |
| 67 | + |
| 68 | +# Run the Git standard I/O client example |
| 69 | +cargo run --example clients_git_stdio |
| 70 | + |
| 71 | +# Run the streamable HTTP client example |
| 72 | +cargo run --example clients_streamable_http |
| 73 | + |
| 74 | +# Run the full-featured standard I/O client example |
| 75 | +cargo run --example clients_everything_stdio |
| 76 | + |
| 77 | +# Run the client collection example |
| 78 | +cargo run --example clients_collection |
| 79 | + |
| 80 | +# Run the OAuth client example |
| 81 | +cargo run --example clients_oauth_client |
| 82 | +``` |
| 83 | + |
| 84 | +## Dependencies |
| 85 | + |
| 86 | +These examples use the following main dependencies: |
| 87 | + |
| 88 | +- `rmcp`: Rust implementation of the MCP client library |
| 89 | +- `tokio`: Asynchronous runtime |
| 90 | +- `serde` and `serde_json`: For JSON serialization and deserialization |
| 91 | +- `tracing` and `tracing-subscriber`: For logging, not must, only for logging |
| 92 | +- `anyhow`: Error handling, not must, only for error handling |
| 93 | +- `axum`: For the OAuth callback HTTP server (used only in the OAuth example) |
| 94 | +- `reqwest`: HTTP client library (used for OAuth and streamable HTTP transport) |
0 commit comments