Skip to content

Commit a11b91c

Browse files
committed
added readme.md
1 parent 7a61b5c commit a11b91c

File tree

1 file changed

+167
-2
lines changed

1 file changed

+167
-2
lines changed

README.md

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The CHUK Tool Processor is a Python library designed to handle the execution of
1010
2. **Executing tools** with proper isolation and error handling
1111
3. **Managing tool executions** with retry logic, caching, and rate limiting
1212
4. **Monitoring tool usage** with comprehensive logging
13+
5. **MCP (Model Context Protocol) Integration** for remote tool execution
1314

1415
## Features
1516

@@ -23,6 +24,7 @@ The CHUK Tool Processor is a Python library designed to handle the execution of
2324
- **Retry Logic**: Automatically retry transient failures with exponential backoff
2425
- **Structured Logging**: Comprehensive logging system for debugging and monitoring
2526
- **Plugin Discovery**: Dynamically discover and load plugins from packages
27+
- **MCP Integration**: Connect to and execute remote tools via Model Context Protocol
2628

2729
## Installation
2830

@@ -90,6 +92,159 @@ if __name__ == "__main__":
9092
asyncio.run(main())
9193
```
9294

95+
## MCP Integration
96+
97+
The CHUK Tool Processor supports Model Context Protocol (MCP) for connecting to remote tool servers. This enables distributed tool execution and integration with third-party services.
98+
99+
### MCP with Stdio Transport
100+
101+
```python
102+
import asyncio
103+
from chuk_tool_processor.mcp import setup_mcp_stdio
104+
105+
async def main():
106+
# Configure MCP server
107+
config_file = "server_config.json"
108+
servers = ["echo", "calculator", "search"]
109+
server_names = {0: "echo", 1: "calculator", 2: "search"}
110+
111+
# Setup MCP with stdio transport
112+
processor, stream_manager = await setup_mcp_stdio(
113+
config_file=config_file,
114+
servers=servers,
115+
server_names=server_names,
116+
namespace="mcp", # All tools will be registered under this namespace
117+
enable_caching=True,
118+
enable_retries=True
119+
)
120+
121+
# Process text with MCP tool calls
122+
llm_text = """
123+
Let me echo your message using the MCP server.
124+
125+
<tool name="mcp.echo" args='{"message": "Hello from MCP!"}'/>
126+
"""
127+
128+
results = await processor.process_text(llm_text)
129+
130+
for result in results:
131+
print(f"Tool: {result.tool}")
132+
print(f"Result: {result.result}")
133+
134+
# Clean up
135+
await stream_manager.close()
136+
137+
if __name__ == "__main__":
138+
asyncio.run(main())
139+
```
140+
141+
### MCP Server Configuration
142+
143+
Create a server configuration file (`server_config.json`):
144+
145+
```json
146+
{
147+
"mcpServers": {
148+
"echo": {
149+
"command": "uv",
150+
"args": ["--directory", "/path/to/echo-server", "run", "src/echo_server/main.py"]
151+
},
152+
"calculator": {
153+
"command": "node",
154+
"args": ["/path/to/calculator-server/index.js"]
155+
},
156+
"search": {
157+
"command": "python",
158+
"args": ["/path/to/search-server/main.py"]
159+
}
160+
}
161+
}
162+
```
163+
164+
### Namespaced Tool Access
165+
166+
MCP tools are automatically registered in both their namespace and the default namespace:
167+
168+
```python
169+
# These are equivalent:
170+
<tool name="echo" args='{"message": "Hello"}'/>
171+
<tool name="mcp.echo" args='{"message": "Hello"}'/>
172+
```
173+
174+
### MCP with SSE Transport
175+
176+
```python
177+
import asyncio
178+
from chuk_tool_processor.mcp import setup_mcp_sse
179+
180+
async def main():
181+
# Configure SSE servers
182+
sse_servers = [
183+
{
184+
"name": "weather",
185+
"url": "https://api.example.com/sse/weather",
186+
"api_key": "your_api_key"
187+
},
188+
{
189+
"name": "geocoding",
190+
"url": "https://api.example.com/sse/geocoding"
191+
}
192+
]
193+
194+
# Setup MCP with SSE transport
195+
processor, stream_manager = await setup_mcp_sse(
196+
servers=sse_servers,
197+
server_names={0: "weather", 1: "geocoding"},
198+
namespace="remote",
199+
enable_caching=True
200+
)
201+
202+
# Process tool calls
203+
llm_text = """
204+
Get the weather for New York.
205+
206+
<tool name="remote.weather" args='{"location": "New York", "units": "imperial"}'/>
207+
"""
208+
209+
results = await processor.process_text(llm_text)
210+
211+
await stream_manager.close()
212+
```
213+
214+
### MCP Stream Manager
215+
216+
The `StreamManager` class handles all MCP communication:
217+
218+
```python
219+
from chuk_tool_processor.mcp.stream_manager import StreamManager
220+
221+
# Create and initialize
222+
stream_manager = await StreamManager.create(
223+
config_file="config.json",
224+
servers=["echo", "search"],
225+
transport_type="stdio"
226+
)
227+
228+
# Get available tools
229+
tools = stream_manager.get_all_tools()
230+
for tool in tools:
231+
print(f"Tool: {tool['name']}")
232+
233+
# Get server information
234+
server_info = stream_manager.get_server_info()
235+
for server in server_info:
236+
print(f"Server: {server['name']}, Status: {server['status']}")
237+
238+
# Call a tool directly
239+
result = await stream_manager.call_tool(
240+
tool_name="echo",
241+
arguments={"message": "Hello"}
242+
)
243+
244+
# Clean up
245+
await stream_manager.close()
246+
```
247+
93248
## Advanced Usage
94249

95250
### Using Decorators for Tool Configuration
@@ -248,11 +403,16 @@ The tool processor has several key components organized into a modular structure
248403
- `plugins/discovery.py`: Plugin discovery mechanism
249404
- `plugins/parsers/`: Parser plugins for different formats
250405

251-
5. **Utils**: Shared utilities
406+
5. **MCP Integration**: Model Context Protocol support
407+
- `mcp/stream_manager.py`: Manages MCP server connections
408+
- `mcp/transport/`: Transport implementations (stdio, SSE)
409+
- `mcp/setup_mcp_*.py`: Easy setup functions for MCP integration
410+
411+
6. **Utils**: Shared utilities
252412
- `utils/logging.py`: Structured logging system
253413
- `utils/validation.py`: Argument and result validation
254414

255-
6. **Core**: Central components
415+
7. **Core**: Central components
256416
- `core/processor.py`: Main processor for handling tool calls
257417
- `core/exceptions.py`: Exception hierarchy
258418

@@ -263,6 +423,8 @@ The repository includes several example scripts:
263423
- `examples/tool_registry_example.py`: Demonstrates tool registration and usage
264424
- `examples/plugin_example.py`: Shows how to create and use custom plugins
265425
- `examples/tool_calling_example_usage.py`: Basic example demonstrating tool execution
426+
- `examples/mcp_stdio_example.py`: MCP stdio transport demonstration
427+
- `examples/mcp_stdio_example_calling_usage.py`: Complete MCP integration example
266428

267429
Run examples with:
268430

@@ -276,6 +438,9 @@ uv run examples/plugin_example.py
276438
# Tool execution example
277439
uv run examples/tool_calling_example_usage.py
278440

441+
# MCP example
442+
uv run examples/mcp_stdio_example.py
443+
279444
# Enable debug logging
280445
LOGLEVEL=DEBUG uv run examples/tool_calling_example_usage.py
281446
```

0 commit comments

Comments
 (0)