RMCP Agent
A powerful Rust library that extends langchain-rust with support for Model Context Protocol (MCP) tool integration and streaming tool execution capabilities.
Features
- 🔗 Langchain-rust Extension: Built as an extension to the langchain-rust ecosystem, providing seamless integration with existing langchain workflows
- ⚡ RMCP Tool Integration: Native support for Model Context Protocol (MCP) tools via SSE transport, enabling AI agents to interact with mathematical calculations and custom services
- 🌊 Streaming Tool Execution: Real-time streaming of tool execution results with detailed progress tracking and error handling
- 🔧 Flexible Agent Builder: Easy-to-use builder pattern for constructing agents with custom instructions and tool configurations
Quick Start
Running the Examples
The repository includes comprehensive examples that demonstrate the capabilities of RMCP Agent:
1. MCP Demo Server
Start the demo server that provides basic mathematical tools:
This will start a server on http://127.0.0.1:8000 with SSE endpoint at /sse providing tools:
- sum: Add two integers
- sub: Subtract two integers
- factorial: Calculate factorial of a positive integer (1-20)
2. Streaming Tool Usage
Run the streaming client that demonstrates real-time tool execution:
# Create .env file with your API credentials
# Edit examples/.env with your OPENAI_API_KEY and OPENAI_API_BASE
This demonstrates the library's capabilities with a real mathematical computation task: "Calculate 3 + 5 - 1, then find the factorial of the result."
Expected Output
When running the streaming example, you'll see output like:
🚀 Demonstrating RMCP tool usage...
## Execution Plan
1. Calculate the sum of 3 and 5 using the `sum` function.
2. Subtract 1 from the result using the `sub` function.
3. Calculate the factorial of the result using the `factorial` function.
## Tool Selection
- sum function: To add 3 and 5
- sub function: To subtract 1 from the sum
- factorial function: To compute the factorial of the final result
## Task Execution
🏗️ sum calling...
🔧 Tool executed: sum
💡 Result: 8
🏗️ sub calling...
🔧 Tool executed: sub
💡 Result: 7
🏗️ factorial calling...
🔧 Tool executed: factorial
💡 Result: 5040
## Results Summary
Based on tool execution results: sum=8, sub=7, factorial=5040
Task completion status: Successfully completed all calculations
Final answer: The result of 3 + 5 - 1 is 7, and the factorial of 7 is 5040
✅ Execution completed
📋 Tool execution results summary:
sum (call_abc123): 8
sub (call_def456): 7
factorial (call_ghi789): 5040
🎉 Demo completed!
Basic Usage
Here's the complete example from streaming_with_rmcp_tools.rs:
use Pin;
use Arc;
use ;
use ChainError;
use prompt_args;
use StreamData;
use ;
use RunningService;
use SseClientTransport;
use ;
use OpenAIMcpAgentBuilder;
use OpenAIMcpAgentExecutor;
async
async
Creating Your Own MCP Tools
You can create custom MCP tools using the provided server framework. Here's an example from rmcp_demo_server.rs:
use ;
use ;
Architecture
Core Components
OpenAIMcpAgentBuilder: Builder for creating agents with MCP tool integrationOpenAIMcpAgentExecutor: Executor that handles streaming tool calls and agent iterationsIntermediateStep: Trait for handling intermediate reasoning steps- Tool Integration: Seamless integration with RMCP tools via Model Context Protocol
Streaming Flow
User Input → Agent Planning → Tool Calls → Streaming Execution → Results
↑ ↓
└─────────── Iterative Refinement ←────────────────────────┘
Advanced Features
Custom Prompt Engineering
The library supports sophisticated prompt engineering to control agent behavior:
let prefix = "
You are a professional AI assistant. For every task, you must strictly follow this workflow:
## MANDATORY WORKFLOW - DO NOT SKIP ANY STEPS:
**Step 1: Create Execution Plan (REQUIRED)**
- MUST start your response with '## Execution Plan'
- List detailed step-by-step execution plan
**Step 2: Tool Selection (REQUIRED)**
- Specify which tools you will use and why
**Step 3: Task Execution (REQUIRED)**
- Execute the plan using selected tools
- **IMPORTANT: After tool execution completes, proceed directly to Step 4**
**Step 4: Results Summary (REQUIRED)**
- Summarize execution results
- Verify task completion status
";
let agent_builder = new
.prefix;
Real-time Tool Monitoring
The library provides detailed real-time feedback:
- 🏗️ Tool Calling:
sum calling... - 🔧 Tool Results:
Tool executed: sum - Result: 8 - 📋 Summary: Complete execution summary with all tool call results
- 🚨 Error Handling: Detailed error messages for debugging
Current Limitations & Roadmap
🚧 Current Limitations
- MCP Transport: Currently only supports SSE (Server-Sent Events) transport for MCP integration
- Other transport methods (Streamable HTTP, stdio) are planned for future releases
- Deep Thinking: Advanced reasoning and deep thinking capabilities are not yet supported
- Planning to integrate with models that support chain-of-thought and step-by-step reasoning
Requirements
- Rust 2021 edition or later
- Tokio runtime for async support
- Valid OpenAI API key (or compatible API endpoint)
- MCP server endpoints for tool integration
- Environment variables:
OPENAI_API_KEY: Your OpenAI API keyOPENAI_API_BASE: Your OpenAI API base URL (for custom endpoints)
Dependencies
Key dependencies include:
langchain-rust: Core langchain functionalityrmcp: Model Context Protocol implementationtokio: Async runtimefutures-util: Stream processingserde: Serialization support
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built on top of langchain-rust
- Powered by Model Context Protocol (MCP)