{"slug": "ai-mcp-demo-py", "title": "ai-mcp-demo.py", "summary": "The provided code demonstrates a simple MCP (Model Context Protocol) server named \"host info mcp\" built using the `FastMCP` library. It defines a single tool, `get_host_info`, which collects and returns system information—including OS details, memory, CPU count, and CPU model—as a formatted JSON string. The server is configured to run over standard I/O (stdio) and registers the tool for use by an MCP client.", "body_md": "ai-mcp-demo.py\n\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\n# main.py\n\nfrom mcp.server.fastmcp import FastMCP\n\nimport tools\n\nmcp = FastMCP(\"host info mcp\")\n\nmcp.add_tool(tools.get_host_info)\n\n@mcp.tool()\n\ndef foo():\n\n    return \"\"\n\ndef main():\n\n    mcp.run(\"stdio\") # sse\n\nif __name__ == \"__main__\":\n\n    main()\n\n \n\n# tools.py\n\nimport platform\n\nimport psutil\n\nimport subprocess\n\nimport json\n\ndef get_host_info() -> str:\n\n    \"\"\"get host information\n\n    Returns:\n\n        str: the host information in JSON string\n\n    \"\"\"\n\n    info: dict[str, str] = {\n\n        \"system\": platform.system(),\n\n        \"release\": platform.release(),\n\n        \"machine\": platform.machine(),\n\n        \"processor\": platform.processor(),\n\n        \"memory_gb\": str(round(psutil.virtual_memory().total / (1024**3), 2)),\n\n    }\n\n    cpu_count = psutil.cpu_count(logical=True)\n\n    if cpu_count is None:\n\n        info[\"cpu_count\"] = \"-1\"\n\n    else:\n\n        info[\"cpu_count\"] = str(cpu_count)\n\n \n\n    try:\n\n        cpu_model = subprocess.check_output(\n\n            [\"sysctl\", \"-n\", \"machdep.cpu.brand_string\"]\n\n        ).decode().strip()\n\n        info[\"cpu_model\"] = cpu_model\n\n    except Exception:\n\n        info[\"cpu_model\"] = \"Unknown\"\n\n    return json.dumps(info, indent=4)\n\nif __name__ == '__main__':\n\n    print(get_host_info())", "url": "https://wpnews.pro/news/ai-mcp-demo-py", "canonical_source": "https://gist.github.com/cradiator/76629158bec2140368fa8ac1796f4814", "published_at": "2025-05-10 12:53:04+00:00", "updated_at": "2026-05-22 13:10:00.320265+00:00", "lang": "en", "topics": ["developer-tools"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/ai-mcp-demo-py", "markdown": "https://wpnews.pro/news/ai-mcp-demo-py.md", "text": "https://wpnews.pro/news/ai-mcp-demo-py.txt", "jsonld": "https://wpnews.pro/news/ai-mcp-demo-py.jsonld"}}