BluestoneApps MCP Remote Server
by lallen30
#!/usr/bin/env python3
"""
MCP Bridge (STDIO) - A bridge between Windsurf and the remote MCP server using stdio transport.
"""
import sys
import json
import urllib.request
import urllib.error
import base64
import os
import time
# Remote server configuration
REMOTE_URL = "http://107.191.37.244:5051/jsonrpc"
USERNAME = "admin"
PASSWORD = "n2hXUijptRwpe9v6wZ37yOgEx4P8w3ofDRO0ko4A"
AUTH_HEADER = f"Basic {base64.b64encode(f'{USERNAME}:{PASSWORD}'.encode()).decode()}"
# Create a log file
LOG_FILE = "/tmp/mcp_bridge_stdio.log"
with open(LOG_FILE, "w") as f:
f.write(f"MCP Bridge STDIO started at {time.time()}\n")
def log(message):
"""Log a message to the log file."""
with open(LOG_FILE, "a") as f:
f.write(f"{message}\n")
def forward_request(request_data):
"""Forward a request to the remote server."""
log(f"Forwarding request: {request_data.get('method')}")
headers = {
"Content-Type": "application/json",
"Authorization": AUTH_HEADER
}
try:
# Prepare the request
data = json.dumps(request_data).encode('utf-8')
req = urllib.request.Request(REMOTE_URL, data=data, headers=headers, method="POST")
# Send the request
with urllib.request.urlopen(req) as response:
response_text = response.read().decode('utf-8')
log(f"Received response for {request_data.get('method')}: {response_text[:100]}...")
response_data = json.loads(response_text)
# Handle tools/call responses
if request_data.get('method') == 'tools/call':
if 'result' in response_data:
# Check if it's already an object
if isinstance(response_data['result'], str):
log("Converting string result to object format")
# Windsurf expects an object with a result property
response_data['result'] = {
"result": response_data['result']
}
return response_data
except Exception as e:
log(f"Error: {e}")
return {
"jsonrpc": "2.0",
"error": {
"code": -32000,
"message": f"Error: {e}"
},
"id": request_data.get("id")
}
def main():
"""Main function."""
log("Starting MCP Bridge STDIO")
# Test the connection
test_request = {
"jsonrpc": "2.0",
"method": "tools/list",
"id": 0
}
log("Testing connection to remote server...")
test_response = forward_request(test_request)
if "error" in test_response:
log(f"Failed to connect to remote server: {test_response.get('error')}")
else:
log("Successfully connected to remote server")
# Process requests from stdin
while True:
try:
# Read a line from stdin
line = sys.stdin.readline()
if not line:
log("End of input, exiting")
break
# Parse the request
request_data = json.loads(line)
log(f"Received request: {request_data.get('method')}")
# Forward the request to the remote server
response_data = forward_request(request_data)
# Write the response to stdout
sys.stdout.write(json.dumps(response_data) + "\n")
sys.stdout.flush()
log(f"Sent response for: {request_data.get('method')}")
except json.JSONDecodeError as e:
log(f"Invalid JSON: {line} - {e}")
sys.stdout.write(json.dumps({
"jsonrpc": "2.0",
"error": {
"code": -32700,
"message": "Parse error"
},
"id": None
}) + "\n")
sys.stdout.flush()
except Exception as e:
log(f"Unexpected error: {e}")
sys.stdout.write(json.dumps({
"jsonrpc": "2.0",
"error": {
"code": -32000,
"message": f"Unexpected error: {e}"
},
"id": None
}) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()