BareGit
"""Small stdlib-only MCP stdio example: addition, a resource, and a prompt.

This is an example server, not an MCP server framework. Stdout contains
only newline-delimited JSON-RPC frames; diagnostics belong on stderr.
"""

import json
import sys


def dispatch(method, params):
    if method == "initialize":
        return {
            "protocolVersion": "2025-06-18",
            "capabilities": {"tools": {}, "resources": {}, "prompts": {}},
            "serverInfo": {"name": "agent-smith-example", "version": "1.0"},
        }
    if method == "ping":
        return {}
    if method == "tools/list":
        return {"tools": [{
            "name": "add",
            "description": "Add two numbers exactly.",
            "inputSchema": {
                "type": "object",
                "properties": {"a": {"type": "number"},
                               "b": {"type": "number"}},
                "required": ["a", "b"],
                "additionalProperties": False,
            },
        }]}
    if method == "tools/call":
        args = params.get("arguments", {})
        if params.get("name") != "add":
            raise ValueError("Unknown tool")
        a, b = args.get("a"), args.get("b")
        if (isinstance(a, bool) or isinstance(b, bool)
                or not isinstance(a, (int, float))
                or not isinstance(b, (int, float))):
            return {"isError": True,
                    "content": [{"type": "text",
                                 "text": "a and b must be numbers"}]}
        result = a + b
        return {"content": [{"type": "text", "text": str(result)}],
                "structuredContent": {"sum": result}}
    if method == "resources/list":
        return {"resources": [{"uri": "example://help", "name": "Help",
                               "mimeType": "text/plain"}]}
    if method == "resources/read":
        if params.get("uri") != "example://help":
            raise ValueError("Unknown resource")
        return {"contents": [{"uri": "example://help",
                              "text": "The add tool adds two numbers."}]}
    if method == "prompts/list":
        return {"prompts": [{"name": "greet", "description": "Greet a name",
                             "arguments": [{"name": "name",
                                            "required": True}]}]}
    if method == "prompts/get":
        if params.get("name") != "greet":
            raise ValueError("Unknown prompt")
        name = params.get("arguments", {}).get("name", "friend")
        return {"messages": [{"role": "user", "content": {
            "type": "text", "text": f"Say hello to {name}."}}]}
    raise LookupError("Method not found")


def main():
    for line in sys.stdin:
        request = json.loads(line)
        if "id" not in request or "method" not in request:
            continue
        reply = {"jsonrpc": "2.0", "id": request["id"]}
        try:
            reply["result"] = dispatch(request["method"],
                                       request.get("params", {}))
        except (ValueError, KeyError) as error:
            reply["error"] = {"code": -32602, "message": str(error)}
        except LookupError as error:
            reply["error"] = {"code": -32601, "message": str(error)}
        print(json.dumps(reply), flush=True)


if __name__ == "__main__":
    main()