Skip to content

Commit 4cd28cd

Browse files
committed
Complete analysis and insights documentation
- Added comprehensive insights analysis document - Documented technical achievements vs integration failures - Identified key lessons: integration-first testing, assumption validation - Preserved all working code and architecture for future use - Ready for next phase: study working MCP implementations
1 parent 2c99475 commit 4cd28cd

10 files changed

+14861
-9
lines changed

MCP_TESTING_CHECKLIST.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# MCP Testing Checklist - Proper Development Process
2+
3+
## ❌ WRONG WAY (What We Did)
4+
- [x] Test with manual JSON-RPC: `echo '{}' | python3 server.py`
5+
- [x] Test interactive mode: `python3 server.py --interactive`
6+
- [x] Test direct tool calls: `python3 debug.py tool_name`
7+
- [x] Assume stdio transport works with Q CLI
8+
- [x] Add features before basic connection works
9+
10+
## ✅ RIGHT WAY (What We Should Do)
11+
12+
### Phase 1: Basic Q CLI Integration
13+
- [ ] Enable Q CLI debug logging: `export Q_LOG_LEVEL=trace`
14+
- [ ] Study existing working MCP servers in Q CLI
15+
- [ ] Create minimal server that only handles `initialize`
16+
- [ ] Test ONLY in Q CLI context: `q mcp add --name test --command "server.py"`
17+
- [ ] Verify server loads without "Transport closed" error
18+
19+
### Phase 2: Protocol Compliance
20+
- [ ] Study Q CLI MCP transport mechanism (not stdio)
21+
- [ ] Implement proper transport layer
22+
- [ ] Add server-side logging for all Q CLI interactions
23+
- [ ] Test `initialize``tools/list``tools/call` sequence
24+
- [ ] Verify JSON-RPC responses match Q CLI expectations
25+
26+
### Phase 3: Functionality Testing
27+
- [ ] Add one tool at a time
28+
- [ ] Test each tool in Q CLI after adding
29+
- [ ] Verify tool responses display correctly in Q CLI
30+
- [ ] Test error handling in Q CLI context
31+
- [ ] Verify server stability during Q CLI sessions
32+
33+
### Phase 4: Production Readiness
34+
- [ ] Load testing with multiple Q CLI sessions
35+
- [ ] Error recovery testing
36+
- [ ] Performance benchmarking in Q CLI
37+
- [ ] Documentation for Q CLI MCP development
38+
- [ ] Automated Q CLI integration tests
39+
40+
## 🔍 Debug Commands
41+
42+
```bash
43+
# Enable Q CLI debugging
44+
export Q_LOG_LEVEL=trace
45+
46+
# Check Q CLI logs
47+
find /tmp -name "*qlog*" -o -name "*qchat*" 2>/dev/null
48+
tail -f /tmp/qlog/qchat.log
49+
50+
# List working MCP servers
51+
q mcp list
52+
53+
# Test MCP server loading
54+
q mcp add --name test-server --command "/path/to/server.py"
55+
56+
# Remove failed server
57+
q mcp remove --name test-server
58+
```
59+
60+
## 🚨 Red Flags (Stop Development)
61+
- "Transport closed" errors
62+
- Server loads but tools don't work in Q CLI
63+
- Manual testing works but Q CLI fails
64+
- No visibility into Q CLI interactions
65+
- Adding features before basic connection works
66+
67+
## ✅ Green Flags (Continue Development)
68+
- Server loads in Q CLI without errors
69+
- Tools list appears in Q CLI
70+
- Tool calls work and display results
71+
- Server remains stable during Q CLI use
72+
- Comprehensive logging shows all interactions
73+
74+
---
75+
**Created**: 2025-09-21 03:15:48 UTC-07:00
76+
**Purpose**: Prevent future MCP development failures

core/mcp_connection_manager.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python3
2+
"""
3+
MCP Connection Manager - Handle connection issues and provide direct access
4+
"""
5+
import json
6+
import subprocess
7+
import asyncio
8+
import sys
9+
import os
10+
11+
class MCPConnectionManager:
12+
"""Manage MCP connections and provide direct tool access"""
13+
14+
def __init__(self, server_path="/home/petr/jetson/core/mcp_server_enhanced.py"):
15+
self.server_path = server_path
16+
self.server_dir = os.path.dirname(server_path)
17+
18+
def call_tool_direct(self, tool_name: str, arguments: dict = None) -> dict:
19+
"""Call MCP tool directly when connection is down"""
20+
if arguments is None:
21+
arguments = {}
22+
23+
request = {
24+
"jsonrpc": "2.0",
25+
"id": 1,
26+
"method": "tools/call",
27+
"params": {
28+
"name": tool_name,
29+
"arguments": arguments
30+
}
31+
}
32+
33+
try:
34+
# Call server directly
35+
process = subprocess.run(
36+
["python3", self.server_path],
37+
input=json.dumps(request),
38+
capture_output=True,
39+
text=True,
40+
cwd=self.server_dir,
41+
timeout=30
42+
)
43+
44+
if process.returncode == 0 and process.stdout:
45+
result = json.loads(process.stdout.strip())
46+
return result.get("result", {}).get("content", [{}])[0].get("text", "No response")
47+
else:
48+
return f"❌ Direct call failed: {process.stderr}"
49+
50+
except Exception as e:
51+
return f"❌ Connection error: {str(e)}"
52+
53+
def test_system_limits(self):
54+
"""Test all system capabilities"""
55+
print("🚀 Testing Phase 5 System Limits via Direct MCP Connection")
56+
print("=" * 60)
57+
58+
# Test 1: Maximum batch processing
59+
print("\n🔥 Test 1: Maximum Batch Processing")
60+
batch_result = self.call_tool_direct("batch_inference", {
61+
"prompts": [
62+
"Create FastAPI endpoint with JWT auth",
63+
"Write async PostgreSQL connection pool",
64+
"Build WebSocket real-time handler",
65+
"Generate comprehensive error middleware",
66+
"Design microservices architecture",
67+
"Implement Redis caching system",
68+
"Create automated testing pipeline",
69+
"Build Docker multi-stage deployment",
70+
"Optimize Jetson CUDA utilization",
71+
"Design scalable API gateway"
72+
]
73+
})
74+
print(batch_result)
75+
76+
# Test 2: Intelligent model selection
77+
print("\n🎯 Test 2: Intelligent Model Selection")
78+
model_result = self.call_tool_direct("select_optimal_model", {
79+
"prompt": "Real-time speech-to-text on Jetson Orin with CUDA, <100ms latency, 95% accuracy, noisy environment"
80+
})
81+
print(model_result)
82+
83+
# Test 3: Memory limits
84+
print("\n💾 Test 3: Memory Status and Limits")
85+
memory_result = self.call_tool_direct("get_memory_status")
86+
print(memory_result)
87+
88+
# Test 4: System status
89+
print("\n🚀 Test 4: System Status")
90+
status_result = self.call_tool_direct("get_system_status")
91+
print(status_result)
92+
93+
# Test 5: Model management
94+
print("\n🤖 Test 5: Model Management")
95+
models_result = self.call_tool_direct("list_models")
96+
print(models_result)
97+
98+
# Test 6: Hot swap capabilities
99+
print("\n🔄 Test 6: Hot Swap Test")
100+
swap_result = self.call_tool_direct("hot_swap_models", {
101+
"source_model": "llama-7b",
102+
"target_model": "codellama-7b"
103+
})
104+
print(swap_result)
105+
106+
print("\n✅ Phase 5 System Limits Test Complete!")
107+
108+
if __name__ == "__main__":
109+
manager = MCPConnectionManager()
110+
manager.test_system_limits()

core/mcp_server_enhanced.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def handle_request(request):
4343
{"name": "batch_inference", "description": "Multi-prompt processing", "inputSchema": {"type": "object", "properties": {"prompts": {"type": "array"}}, "required": ["prompts"]}},
4444
{"name": "create_agent_session", "description": "Persistent conversations", "inputSchema": {"type": "object", "properties": {"session_id": {"type": "string"}}, "required": ["session_id"]}},
4545
{"name": "reload_mcp_server", "description": "Hot reload MCP server for development", "inputSchema": {"type": "object", "properties": {}, "required": []}},
46-
{"name": "use_hf_mcp", "description": "Direct access to HuggingFace MCP tools", "inputSchema": {"type": "object", "properties": {"tool_name": {"type": "string"}, "arguments": {"type": "object"}}, "required": ["tool_name", "arguments"]}}
46+
{"name": "use_hf_mcp", "description": "Direct access to HuggingFace MCP tools", "inputSchema": {"type": "object", "properties": {"tool_name": {"type": "string"}, "arguments": {"type": "object"}}, "required": ["tool_name", "arguments"]}},
47+
{"name": "list_available_tools", "description": "List all available MCP tools with descriptions", "inputSchema": {"type": "object", "properties": {}, "required": []}}
4748
]
4849
}
4950
}
@@ -69,21 +70,23 @@ def handle_request(request):
6970
prompt = args.get("prompt", "")
7071
thinking_mode = args.get("thinking_mode", "immediate")
7172

72-
# Use real inference engine
73+
# Use real inference engine with actual AI generation
7374
try:
74-
mode_enum = ThinkingMode(thinking_mode) if thinking_mode in [m.value for m in ThinkingMode] else ThinkingMode.IMMEDIATE
75-
response = phase3_engine.generate_text(prompt, thinking_mode=mode_enum)
75+
# Call the real generate_text method that uses HuggingFace API
76+
response = phase3_engine.generate_text(prompt, thinking_mode=thinking_mode)
7677

7778
return {
7879
"jsonrpc": "2.0",
7980
"id": request.get("id"),
80-
"result": {"content": [{"type": "text", "text": f"🧠 {response}"}]}
81+
"result": {"content": [{"type": "text", "text": response}]}
8182
}
8283
except Exception as e:
84+
# Enhanced fallback with actual model selection
85+
selected_model = phase3_engine.select_optimal_model(prompt)
8386
return {
8487
"jsonrpc": "2.0",
8588
"id": request.get("id"),
86-
"result": {"content": [{"type": "text", "text": f"🧠 JetsonMind {thinking_mode} mode: {prompt[:50]}... (simulated)"}]}
89+
"result": {"content": [{"type": "text", "text": f"🧠 {selected_model}: {prompt} → [Generated response would appear here with real models]"}]}
8790
}
8891

8992
elif tool_name == "get_system_status":
@@ -189,6 +192,7 @@ def handle_request(request):
189192
continue
190193

191194
try:
195+
# Use real inference engine for batch processing
192196
response = phase3_engine.generate_text(prompt, thinking_mode=ThinkingMode.IMMEDIATE)
193197
results.append(f"{i+1}. {response}")
194198
except Exception as e:
@@ -275,12 +279,39 @@ def handle_request(request):
275279
"result": {"content": [{"type": "text", "text": f"❌ HF MCP Exception: {str(e)}"}]}
276280
}
277281

282+
elif tool_name == "list_available_tools":
283+
tools_info = [
284+
"🛠️ JetsonMind MCP Tools (13 available):",
285+
"",
286+
"1. list_models - List available AI models",
287+
"2. generate_text - Generate text with thinking modes",
288+
"3. get_system_status - Get system status",
289+
"4. get_memory_status - Get memory status",
290+
"5. manage_model_loading - Load/unload models",
291+
"6. get_model_info - Get detailed model information",
292+
"7. select_optimal_model - AI model recommendation",
293+
"8. hot_swap_models - Instant model swapping",
294+
"9. batch_inference - Multi-prompt processing",
295+
"10. create_agent_session - Persistent conversations",
296+
"11. reload_mcp_server - Hot reload MCP server for development",
297+
"12. use_hf_mcp - Direct access to HuggingFace MCP tools",
298+
"13. list_available_tools - List all available MCP tools with descriptions",
299+
"",
300+
"💡 Usage: Ask Q CLI to 'Use [tool_name]' to invoke any tool"
301+
]
302+
303+
return {
304+
"jsonrpc": "2.0",
305+
"id": request.get("id"),
306+
"result": {"content": [{"type": "text", "text": "\n".join(tools_info)}]}
307+
}
308+
278309
# Tool not found - provide helpful suggestions
279310
available_tools = [
280311
"list_models", "generate_text", "get_system_status", "get_memory_status",
281312
"manage_model_loading", "get_model_info", "select_optimal_model",
282313
"hot_swap_models", "batch_inference", "create_agent_session",
283-
"reload_mcp_server", "use_hf_mcp"
314+
"reload_mcp_server", "use_hf_mcp", "list_available_tools"
284315
]
285316

286317
suggestion_text = f"❌ Tool '{tool_name}' not found.\n\n🛠️ Available JetsonMind MCP Tools:\n"

core/utils/mcp_client_pool.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import asyncio
99
import logging
1010
from typing import Dict, Optional
11-
from mcp.client import Client
11+
from mcp.client.stdio import stdio_client
12+
from mcp.client.session import ClientSession
1213

1314
logger = logging.getLogger("mcp-client-pool")
1415

@@ -66,7 +67,7 @@ async def _connect_to_server(self, server_name: str, config: dict):
6667
logger.error(f"Error connecting to {server_name} server: {e}")
6768
raise
6869

69-
async def get_client(self, server_name: str) -> Client:
70+
async def get_client(self, server_name: str) -> ClientSession:
7071
"""
7172
Get MCP client for specified internal server.
7273

debug.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
"""Quick MCP Debug Access - All Tools"""
3+
4+
import json
5+
import subprocess
6+
import sys
7+
8+
def call_debug_tool(tool_name, params=None):
9+
request = {
10+
"jsonrpc": "2.0",
11+
"id": 1,
12+
"method": "tools/call",
13+
"params": {
14+
"name": tool_name,
15+
"arguments": params or {}
16+
}
17+
}
18+
19+
result = subprocess.run([
20+
'python3', '/home/petr/jetson/mcp_debug_server.py'
21+
], input=json.dumps(request), capture_output=True, text=True)
22+
23+
if result.returncode == 0:
24+
response = json.loads(result.stdout)
25+
if "result" in response:
26+
content = response["result"]["content"][0]["text"]
27+
return json.loads(content)
28+
else:
29+
print(f"Error: {response.get('error')}")
30+
return None
31+
else:
32+
print(f"Failed: {result.stderr}")
33+
return None
34+
35+
def main():
36+
tools = ["debug_status", "hot_reload", "test_all_tools", "system_info", "memory_info", "process_info", "file_check", "run_command", "mcp_health", "error_trace"]
37+
38+
if len(sys.argv) < 2:
39+
print("Usage: python3 debug.py <tool_name> [params_json]")
40+
print("Available tools:", ", ".join(tools))
41+
return
42+
43+
tool_name = sys.argv[1]
44+
params = json.loads(sys.argv[2]) if len(sys.argv) > 2 else {}
45+
46+
result = call_debug_tool(tool_name, params)
47+
if result:
48+
print(json.dumps(result, indent=2))
49+
50+
if __name__ == "__main__":
51+
main()

0 commit comments

Comments
 (0)