Skip to content

Commit 0d52b7d

Browse files
authored
fix(examples): add clients in examples's readme (#225)
Signed-off-by: jokemanfire <[email protected]>
1 parent 4e8686f commit 0d52b7d

File tree

9 files changed

+109
-17
lines changed

9 files changed

+109
-17
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ let quit_reason = server.cancel().await?;
107107
</details>
108108

109109

110-
111-
112110
## Examples
113111

114112
See [examples](examples/README.md)
@@ -118,9 +116,6 @@ See [examples](examples/README.md)
118116
See [oauth_support](docs/OAUTH_SUPPORT.md) for details.
119117

120118

121-
122-
123-
124119
## Related Resources
125120

126121
- [MCP Specification](https://spec.modelcontextprotocol.io/specification/2024-11-05/)

examples/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@
5555

5656
# Client Examples
5757

58-
- [Client SSE](clients/src/sse.rs), using reqwest and eventsource-client.
59-
- [Client stdio](clients/src/std_io.rs), using tokio to spawn child process.
60-
- [Everything](clients/src/everything_stdio.rs), test with `@modelcontextprotocol/server-everything`
61-
- [Collection](clients/src/collection.rs), How to transpose service into dynamic object, so they will have a same type.
58+
see [clients/README.md](clients/README.md)
6259

6360
# Server Examples
6461

examples/clients/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ name = "clients_sse"
3434
path = "src/sse.rs"
3535

3636
[[example]]
37-
name = "clients_std_io"
38-
path = "src/std_io.rs"
37+
name = "clients_git_stdio"
38+
path = "src/git_stdio.rs"
3939

4040
[[example]]
4141
name = "clients_streamable_http"
@@ -50,5 +50,5 @@ name = "clients_collection"
5050
path = "src/collection.rs"
5151

5252
[[example]]
53-
name = "oauth_client"
54-
path = "src/oauth_client.rs"
53+
name = "clients_oauth_client"
54+
path = "src/auth/oauth_client.rs"

examples/clients/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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)

examples/clients/src/collection.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/// This example show how to store multiple clients in a map and call tools on them.
2+
/// into_dyn() is used to convert the service to a dynamic service.
3+
/// For example, you can use this to call tools on a service that is running in a different process.
4+
/// or a service that is running in a different machine.
15
use std::collections::HashMap;
26

37
use anyhow::Result;
@@ -20,7 +24,7 @@ async fn main() -> Result<()> {
2024
.with(tracing_subscriber::fmt::layer())
2125
.init();
2226

23-
let mut client_list = HashMap::new();
27+
let mut clients_map = HashMap::new();
2428
for idx in 0..10 {
2529
let service = ()
2630
.into_dyn()
@@ -30,10 +34,10 @@ async fn main() -> Result<()> {
3034
},
3135
))?)
3236
.await?;
33-
client_list.insert(idx, service);
37+
clients_map.insert(idx, service);
3438
}
3539

36-
for (_, service) in client_list.iter() {
40+
for (_, service) in clients_map.iter() {
3741
// Initialize
3842
let _server_info = service.peer_info();
3943

@@ -48,7 +52,7 @@ async fn main() -> Result<()> {
4852
})
4953
.await?;
5054
}
51-
for (_, service) in client_list {
55+
for (_, service) in clients_map {
5256
service.cancel().await?;
5357
}
5458
Ok(())
File renamed without changes.

examples/servers/src/common/generic_service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct GenericService<DS: DataService> {
4444

4545
#[tool(tool_box)]
4646
impl<DS: DataService> GenericService<DS> {
47+
#[allow(dead_code)]
4748
pub fn new(data_service: DS) -> Self {
4849
Self {
4950
data_service: Arc::new(data_service),
@@ -62,6 +63,7 @@ impl<DS: DataService> GenericService<DS> {
6263
}
6364
}
6465

66+
#[tool(tool_box)]
6567
impl<DS: DataService> ServerHandler for GenericService<DS> {
6668
fn get_info(&self) -> ServerInfo {
6769
ServerInfo {

0 commit comments

Comments
 (0)