Team Ai
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 21d agoView on Hugging Face
0likes1.2kdownloads
mcp_malformed_server.py114 linesDownload Raw Back to fixtures
1#!/usr/bin/env python32"""3MCP server that sends malformed responses and notifications during requests.4"""5import json6import sys7import os8 9def handle_initialize(params, req_id):10    return {11        "jsonrpc": "2.0",12        "id": req_id,13        "result": {14            "protocolVersion": "2024-11-05",15            "capabilities": {"tools": {}},16            "serverInfo": {"name": "malformed-test", "version": "1.0"}17        }18    }19 20def handle_tools_list(params, req_id):21    return {22        "jsonrpc": "2.0",23        "id": req_id,24        "result": {25            "tools": [26                {27                    "name": "echo",28                    "description": "Echo back the input message",29                    "inputSchema": {30                        "type": "object",31                        "properties": {32                            "message": {"type": "string"}33                        }34                    }35                }36            ]37        }38    }39 40def handle_tools_call(params, req_id):41    tool_name = params.get("name")42    arguments = params.get("arguments", {})43 44    if tool_name == "echo":45        message = arguments.get("message", "")46        # Send a notification first (no id field)47        notif = {48            "jsonrpc": "2.0",49            "method": "notifications/progress",50            "params": {"progress": 50, "total": 100}51        }52        sys.stdout.write(json.dumps(notif) + "\n")53        sys.stdout.flush()54        # Then send the actual response55        return {56            "jsonrpc": "2.0",57            "id": req_id,58            "result": {59                "content": [{"type": "text", "text": f"echo: {message}"}]60            }61        }62    else:63        return {64            "jsonrpc": "2.0",65            "id": req_id,66            "error": {"code": -32602, "message": f"Unknown tool: {tool_name}"}67        }68 69HANDLERS = {70    "initialize": handle_initialize,71    "tools/list": handle_tools_list,72    "tools/call": handle_tools_call,73}74 75def main():76    sys.stdout = os.fdopen(sys.stdout.fileno(), "w", buffering=1)77    sys.stderr = os.fdopen(sys.stderr.fileno(), "w", buffering=1)78 79    for line in sys.stdin:80        line = line.strip()81        if not line:82            continue83        try:84            request = json.loads(line)85        except json.JSONDecodeError:86            # Send malformed JSON response87            sys.stdout.write("THIS IS NOT JSON\n")88            sys.stdout.flush()89            continue90 91        method = request.get("method")92        req_id = request.get("id")93        params = request.get("params", {})94 95        # JSON-RPC 2.0: a message without an id is a notification and must not receive a response96        if req_id is None:97            continue98 99        handler = HANDLERS.get(method)100        if handler:101            response = handler(params, req_id)102        else:103            response = {104                "jsonrpc": "2.0",105                "id": req_id,106                "error": {"code": -32601, "message": f"Method not found: {method}"}107            }108 109        sys.stdout.write(json.dumps(response) + "\n")110        sys.stdout.flush()111 112if __name__ == "__main__":113    main()114