import asyncio, json, sys
from pathlib import Path
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def main():
    server = sys.argv[1]
    if server == 'blender':
        params = StdioServerParameters(command='C:/Users/ELR17/.blender-mcp/venv/Scripts/python.exe',
            args=['-c','import blmcp.tools_helpers.connection as c; c._TIMEOUT=2.0; import blmcp; blmcp.main()'],
            env={'BLENDER_PATH':'C:/Program Files/Blender Foundation/Blender 5.2/blender.exe', 'BLENDER_MCP_HOST':'127.0.0.1'})
    else:
        params = StdioServerParameters(command='cmd', args=['/c', 'npx', '-y', '@npgamedev/godot-mcp-server@1.0.0'],
            env={'GODOT_MCP_CONFIG_VERSION':'1','GODOT_MCP_PROJECT_PATH': 'E:/GDRIVE/SERVEUR ET PRODUCTION WEB/2025/REBIRTH/BIG EGG/big-egg-3d'})
    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            if len(sys.argv) == 2:
                result = await session.list_tools()
                for tool in result.tools:
                    if any(word in tool.name for word in ['refresh', 'check', 'diagnostic', 'scene', 'code', 'discover', 'group', 'connection']):
                        print(json.dumps({'name':tool.name,'schema':tool.inputSchema}))
            else:
                source = sys.argv[3] if len(sys.argv) > 3 else '{}'
                if source.startswith('@'):
                    source = Path(source[1:]).read_text(encoding='utf-8')
                result = await session.call_tool(sys.argv[2], json.loads(source))
                print(result.model_dump_json())

asyncio.run(asyncio.wait_for(main(), timeout=45))
