Skip to content

Commit 48fae95

Browse files
Merge pull request #736 from MervinPraison/claude/issue-733-20250705_072344
feat: Add minimal latency tracking as custom tool for MCP servers
2 parents db3dcbe + 005c5c7 commit 48fae95

File tree

6 files changed

+1036
-0
lines changed

6 files changed

+1036
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Minimal Latency Tracking for PraisonAI MCP Server
2+
3+
This solution provides latency tracking for MCP servers without modifying any PraisonAI core files. It's implemented as a custom tool that can be used externally.
4+
5+
## Overview
6+
7+
The latency tracking solution measures three key phases:
8+
1. **Planning Process** - Time taken for agent planning and decision making
9+
2. **Tool Usage** - Time spent executing tools
10+
3. **LLM Answer Generation** - Time spent generating responses
11+
12+
## Quick Start
13+
14+
### Option 1: Use as a Custom Tool
15+
16+
```python
17+
from latency_tracker_tool import latency_tracking_tool
18+
19+
# Add to your agent
20+
agent = Agent(
21+
name="Assistant",
22+
role="Helper",
23+
tools=[latency_tracking_tool]
24+
)
25+
26+
# Track manually
27+
latency_tracking_tool("start", "planning", "request_1")
28+
response = agent.chat("Your query")
29+
latency_tracking_tool("end", "planning", "request_1")
30+
31+
# Get metrics
32+
metrics = latency_tracking_tool("metrics", request_id="request_1")
33+
```
34+
35+
### Option 2: Use Context Managers
36+
37+
```python
38+
from latency_tracker_tool import tracker
39+
40+
# Track with context manager
41+
with tracker.track("planning", "request_1"):
42+
response = agent.chat("Your query")
43+
44+
# Get metrics
45+
metrics = tracker.get_metrics("request_1")
46+
```
47+
48+
### Option 3: Use Wrapper Classes
49+
50+
```python
51+
from latency_tracker_tool import create_tracked_agent
52+
53+
# Create tracked agent class
54+
TrackedAgent = create_tracked_agent(Agent)
55+
56+
# Use like normal agent
57+
agent = TrackedAgent(
58+
name="Assistant",
59+
role="Helper",
60+
request_id="request_1"
61+
)
62+
63+
# Operations are automatically tracked
64+
response = agent.chat("Your query")
65+
```
66+
67+
## MCP Server Integration
68+
69+
### Basic MCP Tracking
70+
71+
```python
72+
from latency_tracker_tool import tracker
73+
74+
def handle_mcp_request(request_data):
75+
request_id = request_data.get('id', 'default')
76+
77+
with tracker.track("total_request", request_id):
78+
# Track planning
79+
with tracker.track("planning", request_id):
80+
plan = create_plan(request_data)
81+
82+
# Track tool usage
83+
with tracker.track("tool_usage", request_id):
84+
tool_results = execute_tools(plan)
85+
86+
# Track LLM generation
87+
with tracker.track("llm_generation", request_id):
88+
response = generate_response(tool_results)
89+
90+
# Include metrics in response
91+
metrics = tracker.get_metrics(request_id)
92+
return {
93+
"response": response,
94+
"latency_metrics": metrics
95+
}
96+
```
97+
98+
### Advanced MCP Server Wrapper
99+
100+
```python
101+
from latency_tracker_tool import tracker
102+
103+
def add_tracking_to_mcp_server(mcp_server):
104+
"""Add tracking to existing MCP server."""
105+
original_handle = mcp_server.handle_request
106+
107+
def tracked_handle(request_data):
108+
request_id = request_data.get('id', 'mcp_request')
109+
110+
with tracker.track("mcp_total", request_id):
111+
response = original_handle(request_data)
112+
113+
return response
114+
115+
mcp_server.handle_request = tracked_handle
116+
return mcp_server
117+
```
118+
119+
## Tools with Built-in Tracking
120+
121+
Create a `tools.py` file in your project root:
122+
123+
```python
124+
from latency_tracker_tool import track_latency, tracker
125+
126+
@track_latency("tool_search", "current_request")
127+
def search_tool(query: str) -> str:
128+
"""Search with automatic latency tracking."""
129+
# Your search logic
130+
return results
131+
132+
def get_latency_report(request_id: str = "current_request") -> str:
133+
"""Get latency metrics as a tool."""
134+
metrics = tracker.get_metrics(request_id)
135+
# Format and return report
136+
return formatted_report
137+
```
138+
139+
## API Reference
140+
141+
### LatencyTracker Class
142+
143+
- `start_timer(phase, request_id)` - Start timing a phase
144+
- `end_timer(phase, request_id)` - End timing and return elapsed time
145+
- `track(phase, request_id)` - Context manager for tracking
146+
- `get_metrics(request_id)` - Get metrics for a request
147+
- `get_summary()` - Get summary of all requests
148+
- `clear(request_id)` - Clear tracking data
149+
150+
### Metrics Format
151+
152+
```json
153+
{
154+
"planning": {
155+
"count": 1,
156+
"total": 1.234,
157+
"average": 1.234,
158+
"min": 1.234,
159+
"max": 1.234,
160+
"latest": 1.234
161+
},
162+
"tool_usage": {
163+
"count": 3,
164+
"total": 0.567,
165+
"average": 0.189,
166+
"min": 0.150,
167+
"max": 0.250,
168+
"latest": 0.167
169+
}
170+
}
171+
```
172+
173+
## Examples
174+
175+
See the following example files:
176+
- `example_latency_tracking.py` - Basic usage examples
177+
- `mcp_server_latency_example.py` - MCP server integration
178+
- `tools_with_latency.py` - Tools with built-in tracking
179+
180+
## Benefits
181+
182+
1. **No Core Modifications** - Works without changing PraisonAI source code
183+
2. **Flexible** - Multiple ways to integrate (tool, decorator, wrapper, context manager)
184+
3. **Thread-Safe** - Supports concurrent requests
185+
4. **Minimal Overhead** - Lightweight tracking with negligible performance impact
186+
5. **Extensible** - Easy to add custom phases and metrics
187+
188+
## Use Cases
189+
190+
1. **Performance Monitoring** - Track and optimize MCP server performance
191+
2. **Debugging** - Identify bottlenecks in request processing
192+
3. **SLA Monitoring** - Ensure response times meet requirements
193+
4. **Capacity Planning** - Understand resource usage patterns
194+
5. **A/B Testing** - Compare performance of different implementations
195+
196+
## Tips
197+
198+
1. Use unique request IDs for concurrent request tracking
199+
2. Clear old tracking data periodically to free memory
200+
3. Add tracking to critical paths only to minimize overhead
201+
4. Use phase names consistently across your application
202+
5. Consider logging metrics for long-term analysis
203+
204+
## Integration with Existing Monitoring
205+
206+
The metrics can be easily exported to monitoring systems:
207+
208+
```python
209+
# Export to Prometheus
210+
def export_to_prometheus():
211+
summary = tracker.get_summary()
212+
# Convert to Prometheus format
213+
214+
# Export to CloudWatch
215+
def export_to_cloudwatch():
216+
metrics = tracker.get_metrics()
217+
# Send to CloudWatch
218+
219+
# Export to custom logging
220+
import json
221+
import logging
222+
223+
def log_metrics(request_id):
224+
metrics = tracker.get_metrics(request_id)
225+
logging.info(f"Latency metrics: {json.dumps(metrics)}")
226+
```

0 commit comments

Comments
 (0)