fix(examples): add clients in examples's readme (#225)
Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
This commit is contained in:
parent
4e8686ff47
commit
0d52b7d24f
9 changed files with 109 additions and 17 deletions
|
|
@ -107,8 +107,6 @@ let quit_reason = server.cancel().await?;
|
|||
</details>
|
||||
|
||||
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
See [examples](examples/README.md)
|
||||
|
|
@ -118,9 +116,6 @@ See [examples](examples/README.md)
|
|||
See [oauth_support](docs/OAUTH_SUPPORT.md) for details.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## Related Resources
|
||||
|
||||
- [MCP Specification](https://spec.modelcontextprotocol.io/specification/2024-11-05/)
|
||||
|
|
|
|||
|
|
@ -55,10 +55,7 @@
|
|||
|
||||
# Client Examples
|
||||
|
||||
- [Client SSE](clients/src/sse.rs), using reqwest and eventsource-client.
|
||||
- [Client stdio](clients/src/std_io.rs), using tokio to spawn child process.
|
||||
- [Everything](clients/src/everything_stdio.rs), test with `@modelcontextprotocol/server-everything`
|
||||
- [Collection](clients/src/collection.rs), How to transpose service into dynamic object, so they will have a same type.
|
||||
see [clients/README.md](clients/README.md)
|
||||
|
||||
# Server Examples
|
||||
|
||||
|
|
|
|||
|
|
@ -34,8 +34,8 @@ name = "clients_sse"
|
|||
path = "src/sse.rs"
|
||||
|
||||
[[example]]
|
||||
name = "clients_std_io"
|
||||
path = "src/std_io.rs"
|
||||
name = "clients_git_stdio"
|
||||
path = "src/git_stdio.rs"
|
||||
|
||||
[[example]]
|
||||
name = "clients_streamable_http"
|
||||
|
|
@ -50,5 +50,5 @@ name = "clients_collection"
|
|||
path = "src/collection.rs"
|
||||
|
||||
[[example]]
|
||||
name = "oauth_client"
|
||||
path = "src/oauth_client.rs"
|
||||
name = "clients_oauth_client"
|
||||
path = "src/auth/oauth_client.rs"
|
||||
94
examples/clients/README.md
Normal file
94
examples/clients/README.md
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
# MCP Client Examples
|
||||
|
||||
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.
|
||||
|
||||
## Example List
|
||||
|
||||
### SSE Client (`sse.rs`)
|
||||
|
||||
A client that communicates with an MCP server using Server-Sent Events (SSE) transport.
|
||||
|
||||
- Connects to an MCP server running at `http://localhost:8000/sse`
|
||||
- Retrieves server information and list of available tools
|
||||
- Calls a tool named "increment"
|
||||
|
||||
### Git Standard I/O Client (`git_stdio.rs`)
|
||||
|
||||
A client that communicates with a Git-related MCP server using standard input/output.
|
||||
|
||||
- Launches the `uvx mcp-server-git` command as a child process
|
||||
- Retrieves server information and list of available tools
|
||||
- Calls the `git_status` tool to check the Git status of the current directory
|
||||
|
||||
### Streamable HTTP Client (`streamable_http.rs`)
|
||||
|
||||
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"
|
||||
|
||||
### Full-Featured Standard I/O Client (`everything_stdio.rs`)
|
||||
|
||||
An example demonstrating all MCP client capabilities.
|
||||
|
||||
- Launches `npx -y @modelcontextprotocol/server-everything` as 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
|
||||
|
||||
### Client Collection (`collection.rs`)
|
||||
|
||||
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
|
||||
|
||||
### OAuth Client (`auth/oauth_client.rs`)
|
||||
|
||||
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
|
||||
|
||||
## How to Run
|
||||
|
||||
Each example can be run using Cargo:
|
||||
|
||||
```bash
|
||||
# Run the SSE client example
|
||||
cargo run --example clients_sse
|
||||
|
||||
# Run the Git standard I/O client example
|
||||
cargo run --example clients_git_stdio
|
||||
|
||||
# Run the streamable HTTP client example
|
||||
cargo run --example clients_streamable_http
|
||||
|
||||
# Run the full-featured standard I/O client example
|
||||
cargo run --example clients_everything_stdio
|
||||
|
||||
# Run the client collection example
|
||||
cargo run --example clients_collection
|
||||
|
||||
# Run the OAuth client example
|
||||
cargo run --example clients_oauth_client
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
These examples use the following main dependencies:
|
||||
|
||||
- `rmcp`: Rust implementation of the MCP client library
|
||||
- `tokio`: Asynchronous runtime
|
||||
- `serde` and `serde_json`: For JSON serialization and deserialization
|
||||
- `tracing` and `tracing-subscriber`: For logging, not must, only for logging
|
||||
- `anyhow`: Error handling, not must, only for error handling
|
||||
- `axum`: For the OAuth callback HTTP server (used only in the OAuth example)
|
||||
- `reqwest`: HTTP client library (used for OAuth and streamable HTTP transport)
|
||||
93
examples/clients/src/auth/callback.html
Normal file
93
examples/clients/src/auth/callback.html
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>OAuth Success</title>
|
||||
<style>
|
||||
:root {
|
||||
--primary-color: #4CAF50;
|
||||
--background-color: #f8f9fa;
|
||||
--text-color: #333;
|
||||
--border-color: #e0e0e0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-color: var(--background-color);
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.container {
|
||||
background: white;
|
||||
padding: 2rem;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
text-align: center;
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
margin: 1rem;
|
||||
}
|
||||
|
||||
.icon {
|
||||
font-size: 4rem;
|
||||
color: var(--primary-color);
|
||||
margin-bottom: 1rem;
|
||||
animation: scaleIn 0.5s ease-out;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: var(--primary-color);
|
||||
margin: 0 0 1rem 0;
|
||||
font-size: 1.8rem;
|
||||
}
|
||||
|
||||
p {
|
||||
margin: 0;
|
||||
line-height: 1.6;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.close-button {
|
||||
margin-top: 1.5rem;
|
||||
padding: 0.75rem 1.5rem;
|
||||
background-color: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
transition: background-color 0.2s;
|
||||
}
|
||||
|
||||
.close-button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
@keyframes scaleIn {
|
||||
from {
|
||||
transform: scale(0);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: scale(1);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="icon">✓</div>
|
||||
<h1>Authorization Success</h1>
|
||||
<p>You have successfully authorized the MCP client. You can now close this window and return to the application.</p>
|
||||
<button class="close-button" onclick="window.close()">Close Window</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
229
examples/clients/src/auth/oauth_client.rs
Normal file
229
examples/clients/src/auth/oauth_client.rs
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use axum::{
|
||||
Router,
|
||||
extract::{Query, State},
|
||||
response::Html,
|
||||
routing::get,
|
||||
};
|
||||
use rmcp::{
|
||||
ServiceExt,
|
||||
model::ClientInfo,
|
||||
transport::{
|
||||
SseClientTransport,
|
||||
auth::{AuthClient, OAuthState},
|
||||
sse_client::SseClientConfig,
|
||||
},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use tokio::{
|
||||
io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter},
|
||||
sync::{Mutex, oneshot},
|
||||
};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
const MCP_SERVER_URL: &str = "http://localhost:3000/mcp";
|
||||
const MCP_REDIRECT_URI: &str = "http://localhost:8080/callback";
|
||||
const MCP_SSE_URL: &str = "http://localhost:3000/mcp/sse";
|
||||
const CALLBACK_PORT: u16 = 8080;
|
||||
const CALLBACK_HTML: &str = include_str!("callback.html");
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
code_receiver: Arc<Mutex<Option<oneshot::Sender<String>>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CallbackParams {
|
||||
code: String,
|
||||
#[allow(dead_code)]
|
||||
state: Option<String>,
|
||||
}
|
||||
|
||||
async fn callback_handler(
|
||||
Query(params): Query<CallbackParams>,
|
||||
State(state): State<AppState>,
|
||||
) -> Html<String> {
|
||||
tracing::info!("Received callback with code: {}", params.code);
|
||||
|
||||
// Send the code to the main thread
|
||||
if let Some(sender) = state.code_receiver.lock().await.take() {
|
||||
let _ = sender.send(params.code);
|
||||
}
|
||||
// Return success page
|
||||
Html(CALLBACK_HTML.to_string())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize logging
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "debug".to_string().into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
// it is a http server for handling callback
|
||||
// Create channel for receiving authorization code
|
||||
let (code_sender, code_receiver) = oneshot::channel::<String>();
|
||||
|
||||
// Create app state
|
||||
let app_state = AppState {
|
||||
code_receiver: Arc::new(Mutex::new(Some(code_sender))),
|
||||
};
|
||||
|
||||
// Start HTTP server for handling callbacks
|
||||
let app = Router::new()
|
||||
.route("/callback", get(callback_handler))
|
||||
.with_state(app_state);
|
||||
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], CALLBACK_PORT));
|
||||
tracing::info!("Starting callback server at: http://{}", addr);
|
||||
|
||||
// Start server in a separate task
|
||||
tokio::spawn(async move {
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
let result = axum::serve(listener, app).await;
|
||||
|
||||
if let Err(e) = result {
|
||||
tracing::error!("Callback server error: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
// Get server URL
|
||||
let server_url = MCP_SERVER_URL.to_string();
|
||||
tracing::info!("Using MCP server URL: {}", server_url);
|
||||
|
||||
// Initialize oauth state machine
|
||||
let mut oauth_state = OAuthState::new(&server_url, None)
|
||||
.await
|
||||
.context("Failed to initialize oauth state machine")?;
|
||||
oauth_state
|
||||
.start_authorization(&["mcp", "profile", "email"], MCP_REDIRECT_URI)
|
||||
.await
|
||||
.context("Failed to start authorization")?;
|
||||
|
||||
// Output authorization URL to user
|
||||
let mut output = BufWriter::new(tokio::io::stdout());
|
||||
output.write_all(b"\n=== MCP OAuth Client ===\n\n").await?;
|
||||
output
|
||||
.write_all(b"Please open the following URL in your browser to authorize:\n\n")
|
||||
.await?;
|
||||
output
|
||||
.write_all(oauth_state.get_authorization_url().await?.as_bytes())
|
||||
.await?;
|
||||
output
|
||||
.write_all(b"\n\nWaiting for browser callback, please do not close this window...\n")
|
||||
.await?;
|
||||
output.flush().await?;
|
||||
|
||||
// Wait for authorization code
|
||||
tracing::info!("Waiting for authorization code...");
|
||||
let auth_code = code_receiver
|
||||
.await
|
||||
.context("Failed to get authorization code")?;
|
||||
tracing::info!("Received authorization code: {}", auth_code);
|
||||
// Exchange code for access token
|
||||
tracing::info!("Exchanging authorization code for access token...");
|
||||
oauth_state
|
||||
.handle_callback(&auth_code)
|
||||
.await
|
||||
.context("Failed to handle callback")?;
|
||||
tracing::info!("Successfully obtained access token");
|
||||
|
||||
output
|
||||
.write_all(b"\nAuthorization successful! Access token obtained.\n\n")
|
||||
.await?;
|
||||
output.flush().await?;
|
||||
|
||||
// Create authorized transport, this transport is authorized by the oauth state machine
|
||||
tracing::info!("Establishing authorized connection to MCP server...");
|
||||
let am = oauth_state
|
||||
.into_authorization_manager()
|
||||
.ok_or_else(|| anyhow::anyhow!("Failed to get authorization manager"))?;
|
||||
let client = AuthClient::new(reqwest::Client::default(), am);
|
||||
let transport = SseClientTransport::start_with_client(
|
||||
client,
|
||||
SseClientConfig {
|
||||
sse_endpoint: MCP_SSE_URL.into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Create client and connect to MCP server
|
||||
let client_service = ClientInfo::default();
|
||||
let client = client_service.serve(transport).await?;
|
||||
tracing::info!("Successfully connected to MCP server");
|
||||
|
||||
// Test API requests
|
||||
output
|
||||
.write_all(b"Fetching available tools from server...\n")
|
||||
.await?;
|
||||
output.flush().await?;
|
||||
|
||||
match client.peer().list_all_tools().await {
|
||||
Ok(tools) => {
|
||||
output
|
||||
.write_all(format!("Available tools: {}\n\n", tools.len()).as_bytes())
|
||||
.await?;
|
||||
for tool in tools {
|
||||
output
|
||||
.write_all(
|
||||
format!(
|
||||
"- {} ({})\n",
|
||||
tool.name,
|
||||
tool.description.unwrap_or_default()
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
output
|
||||
.write_all(format!("Error fetching tools: {}\n", e).as_bytes())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
output
|
||||
.write_all(b"\nFetching available prompts from server...\n")
|
||||
.await?;
|
||||
output.flush().await?;
|
||||
|
||||
match client.peer().list_all_prompts().await {
|
||||
Ok(prompts) => {
|
||||
output
|
||||
.write_all(format!("Available prompts: {}\n\n", prompts.len()).as_bytes())
|
||||
.await?;
|
||||
for prompt in prompts {
|
||||
output
|
||||
.write_all(format!("- {}\n", prompt.name).as_bytes())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
output
|
||||
.write_all(format!("Error fetching prompts: {}\n", e).as_bytes())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
output
|
||||
.write_all(b"\nConnection established successfully. You are now authenticated with the MCP server.\n")
|
||||
.await?;
|
||||
output.flush().await?;
|
||||
|
||||
// Keep the program running, wait for user input to exit
|
||||
output.write_all(b"\nPress Enter to exit...\n").await?;
|
||||
output.flush().await?;
|
||||
|
||||
let mut input = String::new();
|
||||
let mut reader = BufReader::new(tokio::io::stdin());
|
||||
reader.read_line(&mut input).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,3 +1,7 @@
|
|||
/// This example show how to store multiple clients in a map and call tools on them.
|
||||
/// into_dyn() is used to convert the service to a dynamic service.
|
||||
/// For example, you can use this to call tools on a service that is running in a different process.
|
||||
/// or a service that is running in a different machine.
|
||||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::Result;
|
||||
|
|
@ -20,7 +24,7 @@ async fn main() -> Result<()> {
|
|||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let mut client_list = HashMap::new();
|
||||
let mut clients_map = HashMap::new();
|
||||
for idx in 0..10 {
|
||||
let service = ()
|
||||
.into_dyn()
|
||||
|
|
@ -30,10 +34,10 @@ async fn main() -> Result<()> {
|
|||
},
|
||||
))?)
|
||||
.await?;
|
||||
client_list.insert(idx, service);
|
||||
clients_map.insert(idx, service);
|
||||
}
|
||||
|
||||
for (_, service) in client_list.iter() {
|
||||
for (_, service) in clients_map.iter() {
|
||||
// Initialize
|
||||
let _server_info = service.peer_info();
|
||||
|
||||
|
|
@ -48,7 +52,7 @@ async fn main() -> Result<()> {
|
|||
})
|
||||
.await?;
|
||||
}
|
||||
for (_, service) in client_list {
|
||||
for (_, service) in clients_map {
|
||||
service.cancel().await?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
48
examples/clients/src/git_stdio.rs
Normal file
48
examples/clients/src/git_stdio.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use anyhow::Result;
|
||||
use rmcp::{
|
||||
model::CallToolRequestParam,
|
||||
service::ServiceExt,
|
||||
transport::{ConfigureCommandExt, TokioChildProcess},
|
||||
};
|
||||
use tokio::process::Command;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize logging
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| format!("info,{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
let service = ()
|
||||
.serve(TokioChildProcess::new(Command::new("uvx").configure(
|
||||
|cmd| {
|
||||
cmd.arg("mcp-server-git");
|
||||
},
|
||||
))?)
|
||||
.await?;
|
||||
|
||||
// or serve_client((), TokioChildProcess::new(cmd)?).await?;
|
||||
|
||||
// Initialize
|
||||
let server_info = service.peer_info();
|
||||
tracing::info!("Connected to server: {server_info:#?}");
|
||||
|
||||
// List tools
|
||||
let tools = service.list_tools(Default::default()).await?;
|
||||
tracing::info!("Available tools: {tools:#?}");
|
||||
|
||||
// Call tool 'git_status' with arguments = {"repo_path": "."}
|
||||
let tool_result = service
|
||||
.call_tool(CallToolRequestParam {
|
||||
name: "git_status".into(),
|
||||
arguments: serde_json::json!({ "repo_path": "." }).as_object().cloned(),
|
||||
})
|
||||
.await?;
|
||||
tracing::info!("Tool result: {tool_result:#?}");
|
||||
service.cancel().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -44,6 +44,7 @@ pub struct GenericService<DS: DataService> {
|
|||
|
||||
#[tool(tool_box)]
|
||||
impl<DS: DataService> GenericService<DS> {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(data_service: DS) -> Self {
|
||||
Self {
|
||||
data_service: Arc::new(data_service),
|
||||
|
|
@ -62,6 +63,7 @@ impl<DS: DataService> GenericService<DS> {
|
|||
}
|
||||
}
|
||||
|
||||
#[tool(tool_box)]
|
||||
impl<DS: DataService> ServerHandler for GenericService<DS> {
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo {
|
||||
|
|
|
|||
Loading…
Reference in a new issue