BluestoneApps MCP Remote Server

#!/usr/bin/env python3 """ Simple MCP Proxy Server This script acts as a simple proxy between Windsurf and the remote MCP server. It doesn't rely on the fastmcp library, just implements the basic JSON-RPC protocol. """ import sys import json import urllib.request import urllib.error import base64 import traceback # 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()}" # Set up logging to stderr def log(message): """Log a message to stderr.""" print(f"PROXY: {message}", file=sys.stderr) sys.stderr.flush() log("Starting Simple MCP Proxy Server") 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: {response_text[:100]}...") response_data = json.loads(response_text) # Special handling for tools/call responses if request_data.get('method') == 'tools/call' and 'result' in response_data: if isinstance(response_data['result'], str): log("Converting string result to object format") response_data['result'] = { "result": response_data['result'] } return response_data except Exception as e: log(f"Error forwarding request: {e}") log(traceback.format_exc()) return { "jsonrpc": "2.0", "id": request_data.get("id"), "error": { "code": -32603, "message": f"Internal error: {str(e)}" } } def main(): """Main function to process stdin/stdout.""" 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}") log(traceback.format_exc()) 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()