#!/usr/bin/env python3
"""
MCP Server 1429 - Servidor de prueba
"""
import json
import sys

def log(msg):
    """Log to stderr (stdout is used for MCP protocol)"""
    print(f"[1429] {msg}", file=sys.stderr, flush=True)

def send_response(response):
    """Send JSON-RPC response to stdout"""
    json_str = json.dumps(response)
    print(json_str, flush=True)

def main():
    log("MCP Server 1429 iniciado")

    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue

        try:
            request = json.loads(line)
            method = request.get("method", "")
            request_id = request.get("id")

            log(f"Request: {method}")

            # Handle initialize
            if method == "initialize":
                response = {
                    "jsonrpc": "2.0",
                    "id": request_id,
                    "result": {
                        "protocolVersion": "2024-11-05",
                        "capabilities": {
                            "tools": {}
                        },
                        "serverInfo": {
                            "name": "1429",
                            "version": "1.0.0"
                        }
                    }
                }
                send_response(response)

            # Handle initialized notification
            elif method == "notifications/initialized":
                log("Client initialized")

            # Handle tools/list
            elif method == "tools/list":
                response = {
                    "jsonrpc": "2.0",
                    "id": request_id,
                    "result": {
                        "tools": [
                            {
                                "name": "hola",
                                "description": "Saluda y confirma que el MCP esta funcionando",
                                "inputSchema": {
                                    "type": "object",
                                    "properties": {},
                                    "required": []
                                }
                            }
                        ]
                    }
                }
                send_response(response)

            # Handle tools/call
            elif method == "tools/call":
                response = {
                    "jsonrpc": "2.0",
                    "id": request_id,
                    "result": {
                        "content": [
                            {
                                "type": "text",
                                "text": "hola! mcp funcionando"
                            }
                        ]
                    }
                }
                send_response(response)

            else:
                # Unknown method
                if request_id:
                    response = {
                        "jsonrpc": "2.0",
                        "id": request_id,
                        "error": {
                            "code": -32601,
                            "message": f"Method not found: {method}"
                        }
                    }
                    send_response(response)

        except json.JSONDecodeError as e:
            log(f"JSON error: {e}")
        except Exception as e:
            log(f"Error: {e}")

if __name__ == "__main__":
    main()