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.
A client that communicates with a Git-related MCP server using standard input/output.
- Launches the
uvx mcp-server-gitcommand as a child process - Retrieves server information and list of available tools
- Calls the
git_statustool to check the Git status of the current directory
A client that communicates with an MCP server using HTTP streaming transport.
- Connects to an MCP server running at
http://localhost:8000 - Retrieves server information and list of available tools
- Calls a tool named "increment"
An example demonstrating all MCP client capabilities.
- Launches
npx -y @modelcontextprotocol/server-everythingas a child process - Retrieves server information and list of available tools
- Calls various tools, including "echo" and "longRunningOperation"
- Lists and reads available resources
- Lists and retrieves simple and complex prompts
- Lists available resource templates
An example showing how to manage multiple MCP clients.
- Creates 10 clients connected to Git servers
- Stores these clients in a HashMap
- Performs the same sequence of operations on each client
- Uses
into_dyn()to convert services to dynamic services
A client demonstrating how to authenticate with an MCP server using OAuth.
- Starts a local HTTP server to handle OAuth callbacks
- Initializes the OAuth state machine and begins the authorization flow
- Displays the authorization URL and waits for user authorization
- Establishes an authorized connection to the MCP server using the acquired access token
- Demonstrates how to use the authorized connection to retrieve available tools and prompts
A client demonstrating how to use the sampling tool.
- Launches the server example
servers_sampling_stdio - Connects to the server
- Retrieves server information and list of available tools
- Calls the
ask_llmtool
A client that communicates with an MCP server using progress notifications.
- Launches the
cargo run -p mcp-client-examples --example clients_progress_client -- --transport {stdio|http|all}to test the progress notifications - Connects to the server using different transport methods
- Tests the progress notifications
- The http transport should run the server first
Each example can be run using Cargo:
# Run the Git standard I/O client example
cargo run -p mcp-client-examples --example clients_git_stdio
# Run the streamable HTTP client example
cargo run -p mcp-client-examples --example clients_streamable_http
# Run the full-featured standard I/O client example
cargo run -p mcp-client-examples --example clients_everything_stdio
# Run the client collection example
cargo run -p mcp-client-examples --example clients_collection
# Run the OAuth client example
cargo run -p mcp-client-examples --example clients_oauth_client
# Run the sampling standard I/O client example
cargo run -p mcp-client-examples --example clients_sampling_stdioThese examples use the following main dependencies:
rmcp: Rust implementation of the MCP client librarytokio: Asynchronous runtimeserdeandserde_json: For JSON serialization and deserializationtracingandtracing-subscriber: For logging, not must, only for logginganyhow: Error handling, not must, only for error handlingaxum: For the OAuth callback HTTP server (used only in the OAuth example)reqwest: HTTP client library (used for OAuth and streamable HTTP transport)