-
Notifications
You must be signed in to change notification settings - Fork 400
examples: add a simple chat client for example #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "simple-chat-client" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| tokio = { version = "1", features = ["full"] } | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_json = "1.0" | ||
| reqwest = { version = "0.11", features = ["json"] } | ||
| anyhow = "1.0" | ||
| thiserror = "1.0" | ||
| async-trait = "0.1" | ||
| futures = "0.3" | ||
| toml = "0.8" | ||
| rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", features = [ | ||
| "client", | ||
| "transport-child-process", | ||
| "transport-sse", | ||
| ], no-default-features = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Simple Chat Client | ||
|
|
||
| A simple chat client implementation using the Model Context Protocol (MCP) SDK. It just a example for developers to understand how to use the MCP SDK. This example use the easiest way to start a MCP server, and call the tool directly. No need embedding or complex third library or function call(because some models can't support function call).Just add tool in system prompt, and the client will call the tool automatically. | ||
|
|
||
|
|
||
| ## Config | ||
| the config file is in `src/config.toml`. you can change the config to your own.Move the config file to `/etc/simple-chat-client/config.toml` for system-wide configuration. | ||
|
|
||
| ## Usage | ||
|
|
||
| After configuring the config file, you can run the example: | ||
| ```bash | ||
| cargo run --bin simple-chat | ||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::Result; | ||
| use simple_chat_client::{ | ||
| chat::ChatSession, | ||
| client::OpenAIClient, | ||
| config::Config, | ||
| tool::{Tool, ToolSet, get_mcp_tools}, | ||
| }; | ||
|
|
||
| //default config path | ||
| const DEFAULT_CONFIG_PATH: &str = "/etc/simple-chat-client/config.toml"; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<()> { | ||
| // load config | ||
| let config = Config::load(DEFAULT_CONFIG_PATH).await?; | ||
|
|
||
| // create openai client | ||
| let api_key = config | ||
| .openai_key | ||
| .clone() | ||
| .unwrap_or_else(|| std::env::var("OPENAI_API_KEY").expect("need set api key")); | ||
| let url = config.chat_url.clone(); | ||
| println!("url is {:?}", url); | ||
| let openai_client = Arc::new(OpenAIClient::new(api_key, url)); | ||
|
|
||
| // create tool set | ||
| let mut tool_set = ToolSet::default(); | ||
|
|
||
| // load mcp | ||
| if config.mcp.is_some() { | ||
| let mcp_clients = config.create_mcp_clients().await?; | ||
|
|
||
| for (name, client) in mcp_clients { | ||
| println!("loading mcp tools: {}", name); | ||
| let server = client.peer().clone(); | ||
| let tools = get_mcp_tools(server).await?; | ||
|
|
||
| for tool in tools { | ||
| println!("adding tool: {}", tool.name()); | ||
| tool_set.add_tool(tool); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // create chat session | ||
| let mut session = ChatSession::new( | ||
| openai_client, | ||
| tool_set, | ||
| config | ||
| .model_name | ||
| .unwrap_or_else(|| "gpt-4o-mini".to_string()), | ||
| ); | ||
|
|
||
| // build system prompt with tool info | ||
| let mut system_prompt = | ||
| "you are a assistant, you can help user to complete various tasks. you have the following tools to use:\n".to_string(); | ||
|
|
||
| // add tool info to system prompt | ||
| for tool in session.get_tools() { | ||
| system_prompt.push_str(&format!( | ||
| "\ntool name: {}\ndescription: {}\nparameters: {}\n", | ||
| tool.name(), | ||
| tool.description(), | ||
| serde_json::to_string_pretty(&tool.parameters()).unwrap_or_default() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe have an example of error handling instead of hiding serialization issues?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right |
||
| )); | ||
| } | ||
|
|
||
| // add tool call format guidance | ||
| system_prompt.push_str( | ||
| "\nif you need to call tool, please use the following format:\n\ | ||
| Tool: <tool name>\n\ | ||
| Inputs: <inputs>\n", | ||
| ); | ||
|
|
||
| // add system prompt | ||
| session.add_system_prompt(system_prompt); | ||
|
|
||
| // start chat | ||
| session.chat().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for an example app, should use
./config.tomlas to not need escalated privileges to read from/etcThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thks, I 'd like to add a arg, to point the config path.