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
Uses modern discovery and subscriptions/listen, prints the accepted filter,
and consumes tagged notifications until graceful closure or cancellation.
- Run with
cargo run -p mcp-client-examples --example clients_subscriptions_streamhttp - 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
- Shows how to pass a configured reqwest client for OAuth discovery, registration, token exchange, and refresh requests
- 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 the OAuth 2.0 Client Credentials flow from SEP-1046.
- Accepts the server URL, client ID, and client secret as command-line arguments
- Authenticates without an interactive browser or callback server
- Establishes an authorized connection and retrieves the available tools
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 exercises the SEP-2663 Tasks extension lifecycle against servers_task_stdio
(SEP-2663, io.modelcontextprotocol/tasks).
- Spawns
servers_task_stdioas a child process over stdio - Declares the tasks extension in its client capabilities
- Calls
quick_echosynchronously - Calls
slow_sum, receives aCreateTaskResult(resultType: "task"), pollstasks/gethonoringpollIntervalMs, and reads the finalCallToolResultinlined in the completed task
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 OAuth Client Credentials example
cargo run -p mcp-client-examples --example clients_client_credentials -- \
<server_url> <client_id> <client_secret>
# Run the sampling standard I/O client example
cargo run -p mcp-client-examples --example clients_sampling_stdio
# Run the task-based invocation client (drives servers_task_stdio)
cargo run -p mcp-client-examples --example clients_task_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)