-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmtools-mcp-wrapper.sh
More file actions
executable file
·57 lines (47 loc) · 1.83 KB
/
dmtools-mcp-wrapper.sh
File metadata and controls
executable file
·57 lines (47 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# DMTools MCP Command Wrapper
# This script acts as a stdio transport bridge for Gemini CLI to communicate with DMTools MCP server
# Server URL (can be overridden by environment variable)
SERVER_URL="${DMTOOLS_SERVER_URL:-http://localhost:8080}"
# Function to call the HTTP MCP server with proper JSON-RPC format
call_mcp_server() {
local request="$1"
# Forward the complete JSON-RPC request to our HTTP endpoint
response=$(curl -s -X POST "${SERVER_URL}/mcp/" \
-H "Content-Type: application/json" \
-d "$request" \
--max-time 30)
# Check if curl succeeded
if [ $? -eq 0 ] && [ -n "$response" ]; then
echo "$response"
else
# Generate error response if server call failed
local request_id=$(echo "$request" | jq -r '.id // "unknown"' 2>/dev/null || echo "unknown")
echo "{\"jsonrpc\":\"2.0\",\"id\":\"$request_id\",\"error\":{\"code\":-32603,\"message\":\"Server communication failed\"}}"
fi
}
# Function to validate JSON-RPC request
validate_jsonrpc() {
local request="$1"
# Check if it's valid JSON and has required fields
if echo "$request" | jq -e '.jsonrpc and .method and (.id != null)' >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# Read JSON-RPC requests from stdin and forward to HTTP server
while IFS= read -r line; do
# Skip empty lines
[ -z "$line" ] && continue
# Log request to stderr for debugging
echo "DEBUG: Received request: $line" >&2
# Validate JSON-RPC format
if validate_jsonrpc "$line"; then
# Forward valid request to HTTP server
call_mcp_server "$line"
else
# Generate error response for invalid requests
echo "{\"jsonrpc\":\"2.0\",\"id\":null,\"error\":{\"code\":-32600,\"message\":\"Invalid JSON-RPC request\"}}"
fi
done