{"id":39949,"date":"2025-05-26T16:11:20","date_gmt":"2025-05-26T08:11:20","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/39949.html"},"modified":"2025-05-26T16:11:20","modified_gmt":"2025-05-26T08:11:20","slug":"mcp%e5%a4%a9%e6%b0%94%e6%9f%a5%e8%af%a2%e6%9c%8d%e5%8a%a1%e5%99%a8server%e4%b8%8e%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/39949.html","title":{"rendered":"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528"},"content":{"rendered":"<h4>1. MCP\u670d\u52a1\u5668\u6982\u5ff5\u4ecb\u7ecd<\/h4>\n<p>\u6839\u636eMCP\u534f\u8bae\u5b9a\u4e49&#xff0c;Server\u53ef\u4ee5\u63d0\u4f9b\u4e09\u79cd\u7c7b\u578b\u7684\u6807\u51c6\u80fd\u529b&#xff0c;Resources\u3001Tools\u3001Prompts&#xff0c;\u6bcf\u4e2aServer\u53ef\u540c\u65f6\u63d0\u4f9b\u8005\u4e09\u79cd\u7c7b\u578b\u80fd\u529b\u6216\u5176\u4e2d\u4e00\u79cd\u3002<\/p>\n<ul>\n<li>\n<p>**Resources&#xff1a;**\u8d44\u6e90&#xff0c;\u7c7b\u4f3c\u4e8e\u6587\u4ef6\u6570\u636e\u8bfb\u53d6&#xff0c;\u53ef\u4ee5\u662f\u6587\u4ef6\u8d44\u6e90\u6216\u662fAPI\u54cd\u5e94\u8fd4\u56de\u7684\u5185\u5bb9\u3002<\/p>\n<\/li>\n<li>\n<p>**Tools&#xff1a;**\u5de5\u5177&#xff0c;\u7b2c\u4e09\u65b9\u670d\u52a1\u3001\u529f\u80fd\u51fd\u6570&#xff0c;\u901a\u8fc7\u6b64\u53ef\u63a7\u5236LLM\u53ef\u8c03\u7528\u54ea\u4e9b\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>**Prompts&#xff1a;**\u63d0\u793a\u8bcd&#xff0c;\u4e3a\u7528\u6237\u9884\u5148\u5b9a\u4e49\u597d\u7684\u5b8c\u6210\u7279\u5b9a\u4efb\u52a1\u7684\u6a21\u677f\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>2. MCP\u670d\u52a1\u5668\u901a\u8baf\u673a\u5236<\/h4>\n<p>Model Context Protocol&#xff08;MCP&#xff09;\u662f\u4e00\u79cd\u7531 Anthropic \u5f00\u6e90\u7684\u534f\u8bae&#xff0c;\u65e8\u5728\u5c06\u5927\u578b\u8bed\u8a00\u6a21\u578b\u76f4\u63a5\u8fde\u63a5\u81f3\u6570\u636e\u6e90&#xff0c;\u5b9e\u73b0\u65e0\u7f1d\u96c6\u6210\u3002\u6839\u636e MCP \u7684\u89c4\u8303&#xff0c;\u5f53\u524d\u652f\u6301\u4e24\u79cd\u4f20\u8f93\u65b9\u5f0f&#xff1a;\u6807\u51c6\u8f93\u5165\u8f93\u51fa&#xff08;stdio&#xff09;\u548c\u57fa\u4e8e HTTP \u7684\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6&#xff08;SSE&#xff09;\u3002\u800c\u8fd1\u671f&#xff0c;\u5f00\u53d1\u8005\u5728 MCP \u7684 GitHub \u4ed3\u5e93\u4e2d\u63d0\u4ea4\u4e86\u4e00\u9879\u63d0\u6848&#xff0c;\u5efa\u8bae\u91c7\u7528\u201c\u53ef\u6d41\u5f0f\u4f20\u8f93\u7684 HTTP\u201d\u6765\u66ff\u4ee3\u73b0\u6709\u7684 HTTP&#043;SSE \u65b9\u6848\u3002\u6b64\u4e3e\u65e8\u5728\u89e3\u51b3\u5f53\u524d\u8fdc\u7a0b MCP \u4f20\u8f93\u65b9\u5f0f\u7684\u5173\u952e\u9650\u5236&#xff0c;\u540c\u65f6\u4fdd\u7559\u5176\u4f18\u52bf\u3002 HTTP \u548c SSE&#xff08;\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6&#xff09;\u5728\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u4e0a\u5b58\u5728\u660e\u663e\u533a\u522b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u901a\u4fe1\u65b9\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>HTTP&#xff1a;\u91c7\u7528\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f&#xff0c;\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42&#xff0c;\u670d\u52a1\u5668\u8fd4\u56de\u54cd\u5e94&#xff0c;\u6bcf\u6b21\u8bf7\u6c42\u90fd\u662f\u72ec\u7acb\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>SSE&#xff1a;\u5141\u8bb8\u670d\u52a1\u5668\u901a\u8fc7\u5355\u4e2a\u6301\u4e45\u7684 HTTP \u8fde\u63a5&#xff0c;\u6301\u7eed\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6570\u636e&#xff0c;\u5b9e\u73b0\u5b9e\u65f6\u66f4\u65b0\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u7279\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>HTTP&#xff1a;\u6bcf\u6b21\u8bf7\u6c42\u901a\u5e38\u5efa\u7acb\u65b0\u7684\u8fde\u63a5&#xff0c;\u867d\u7136\u5728 HTTP\/1.1 \u4e2d\u5f15\u5165\u4e86\u6301\u4e45\u8fde\u63a5&#xff0c;\u4f46\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4ecd\u662f\u77ed\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>SSE&#xff1a;\u57fa\u4e8e\u957f\u8fde\u63a5&#xff0c;\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u4fdd\u6301\u6301\u7eed\u7684\u8fde\u63a5&#xff0c;\u670d\u52a1\u5668\u53ef\u4ee5\u5728\u4efb\u610f\u65f6\u95f4\u63a8\u9001\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u9002\u7528\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\n<p>HTTP&#xff1a;\u9002\u7528\u4e8e\u4f20\u7edf\u7684\u8bf7\u6c42-\u54cd\u5e94\u573a\u666f&#xff0c;\u5982\u7f51\u9875\u52a0\u8f7d\u3001\u8868\u5355\u63d0\u4ea4\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>SSE&#xff1a;\u9002\u7528\u4e8e\u9700\u8981\u670d\u52a1\u5668\u4e3b\u52a8\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6570\u636e\u7684\u573a\u666f&#xff0c;\u5982\u5b9e\u65f6\u901a\u77e5\u3001\u80a1\u7968\u884c\u60c5\u66f4\u65b0\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;SSE \u4ec5\u652f\u6301\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u7684\u5355\u5411\u901a\u4fe1&#xff0c;\u800c WebSocket \u5219\u652f\u6301\u53cc\u5411\u901a\u4fe1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"169\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250526081117-68342225e8aa2.png\" width=\"1204\" \/><\/p>\n<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c1d\u8bd5\u4e00\u4e2a\u5165\u95e8\u7ea7\u7684\u793a\u4f8b&#xff0c;\u90a3\u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u5929\u6c14\u67e5\u8be2\u7684\u670d\u52a1\u5668\u3002\u901a\u8fc7\u4f7f\u7528OpenWeather API&#xff0c;\u521b\u5efa\u4e00\u4e2a\u80fd\u591f\u5b9e\u65f6\u67e5\u8be2\u5929\u6c14\u7684\u670d\u52a1\u5668&#xff08;server&#xff09;&#xff0c;\u5e76\u4f7f\u7528stdio\u65b9\u5f0f\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"439\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250526081118-68342226442ec.png\" width=\"1213\" \/><\/p>\n<p>api\u7684\u6ce8\u518c\u53c2\u8003:\u00a0<\/p>\n<p>\u96f6\u57fa\u7840\u5f00\u53d1AI\u667a\u80fd\u4f53\u6559\u7a0b&#xff08;5&#xff09;&#xff5c;\u5728\u667a\u80fd\u4f53\u4e2d\u8c03\u7528\u5916\u90e8\u5de5\u5177\u7684\u5b9e\u73b0\u6307\u5357\u2014\u2014\u4ee5\u5929\u6c14\u67e5\u8be2\u4e3a\u4f8b_\u667a\u80fd\u4f53\u5e73\u53f0\u5982\u4f55\u901a\u8fc7\u5bfc\u5165\u5916\u90e8api\u751f\u6210\u5de5\u5177-CSDN\u535a\u5ba2<\/p>\n<p>\u6d4b\u8bd5api&#xff1a;<\/p>\n<p>curl -s &#034;https:\/\/api.openweathermap.org\/data\/2.5\/weather?q&#061;Beijing&amp;appid&#061;&#039;YOUR_API_KEY&#039;&amp;units&#061;metric&amp;lang&#061;zh_cn&#034; <\/p>\n<p>\u00a0\u6ce8\u610f&#xff0c;api\u4e0d\u9700\u8981\u52a0\u5355\u5f15\u53f7<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"140\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250526081118-68342226c131d.png\" width=\"1706\" \/><\/p>\n<p>\u80fd\u591f\u6b63\u5e38\u8f93\u51fa<\/p>\n<\/p>\n<h4>3. \u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668Server\u521b\u5efa\u6d41\u7a0b<\/h4>\n<h5>3.1 \u670d\u52a1\u5668\u4f9d\u8d56\u5b89\u88c5<\/h5>\n<p>\u7531\u4e8e\u6211\u4eec\u9700\u8981\u4f7f\u7528http\u8bf7\u6c42\u6765\u67e5\u8be2\u5929\u6c14&#xff0c;\u56e0\u6b64\u9700\u8981\u5728\u5f53\u524d\u865a\u62df\u73af\u5883\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4f9d\u8d56<\/p>\n<p>uv add mcp httpx &#8211;index-url https:\/\/mirrors.aliyun.com\/pypi\/simple\/ <\/p>\n<h5>3.2 \u670d\u52a1\u5668\u4ee3\u7801\u7f16\u5199<\/h5>\n<p>\u63a5\u4e0b\u6765\u5c1d\u8bd5\u521b\u5efa\u670d\u52a1\u5668\u4ee3\u7801&#xff0c;\u6b64\u65f6MCP\u57fa\u672c\u6267\u884c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"997\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250526081118-68342226e69bb.png\" width=\"1236\" \/><\/p>\n<p>\u5bf9\u5e94server\u670d\u52a1\u5668\u4ee3\u7801\u4e3a&#xff1a;<\/p>\n<p>import json<br \/>\nimport httpx<br \/>\nfrom typing import Any<br \/>\nfrom mcp.server.fastmcp import FastMCP<\/p>\n<p># \u521d\u59cb\u5316 MCP \u670d\u52a1\u5668<br \/>\nmcp &#061; FastMCP(&#034;WeatherServer&#034;)<\/p>\n<p># OpenWeather API \u914d\u7f6e<br \/>\nOPENWEATHER_API_BASE &#061; &#034;https:\/\/api.openweathermap.org\/data\/2.5\/weather&#034;<br \/>\nAPI_KEY &#061; &#034;YOUR_API_KEY&#034;  # \u8bf7\u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684 OpenWeather API Key<br \/>\nUSER_AGENT &#061; &#034;weather-app\/1.0&#034;<\/p>\n<p>async def fetch_weather(city: str) -&gt; dict[str, Any] | None:<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u4ece OpenWeather API \u83b7\u53d6\u5929\u6c14\u4fe1\u606f\u3002<br \/>\n    :param city: \u57ce\u5e02\u540d\u79f0&#xff08;\u9700\u4f7f\u7528\u82f1\u6587&#xff0c;\u5982 Beijing&#xff09;<br \/>\n    :return: \u5929\u6c14\u6570\u636e\u5b57\u5178&#xff1b;\u82e5\u51fa\u9519\u8fd4\u56de\u5305\u542b error \u4fe1\u606f\u7684\u5b57\u5178<br \/>\n    &#034;&#034;&#034;<br \/>\n    params &#061; {<br \/>\n        &#034;q&#034;: city,<br \/>\n        &#034;appid&#034;: API_KEY,<br \/>\n        &#034;units&#034;: &#034;metric&#034;,<br \/>\n        &#034;lang&#034;: &#034;zh_cn&#034;<br \/>\n    }<br \/>\n    headers &#061; {&#034;User-Agent&#034;: USER_AGENT}<\/p>\n<p>    async with httpx.AsyncClient() as client:<br \/>\n        try:<br \/>\n            response &#061; await client.get(OPENWEATHER_API_BASE, params&#061;params, headers&#061;headers, timeout&#061;30.0)<br \/>\n            response.raise_for_status()<br \/>\n            return response.json()  # \u8fd4\u56de\u5b57\u5178\u7c7b\u578b<br \/>\n        except httpx.HTTPStatusError as e:<br \/>\n            return {&#034;error&#034;: f&#034;HTTP \u9519\u8bef: {e.response.status_code}&#034;}<br \/>\n        except Exception as e:<br \/>\n            return {&#034;error&#034;: f&#034;\u8bf7\u6c42\u5931\u8d25: {str(e)}&#034;}<\/p>\n<p>def format_weather(data: dict[str, Any] | str) -&gt; str:<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u5c06\u5929\u6c14\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6613\u8bfb\u6587\u672c\u3002<br \/>\n    :param data: \u5929\u6c14\u6570\u636e&#xff08;\u53ef\u4ee5\u662f\u5b57\u5178\u6216 JSON \u5b57\u7b26\u4e32&#xff09;<br \/>\n    :return: \u683c\u5f0f\u5316\u540e\u7684\u5929\u6c14\u4fe1\u606f\u5b57\u7b26\u4e32<br \/>\n    &#034;&#034;&#034;<br \/>\n    # \u5982\u679c\u4f20\u5165\u7684\u662f\u5b57\u7b26\u4e32&#xff0c;\u5219\u5148\u8f6c\u6362\u4e3a\u5b57\u5178<br \/>\n    if isinstance(data, str):<br \/>\n        try:<br \/>\n            data &#061; json.loads(data)<br \/>\n        except Exception as e:<br \/>\n            return f&#034;\u65e0\u6cd5\u89e3\u6790\u5929\u6c14\u6570\u636e: {e}&#034;<\/p>\n<p>    # \u5982\u679c\u6570\u636e\u4e2d\u5305\u542b\u9519\u8bef\u4fe1\u606f&#xff0c;\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\u63d0\u793a<br \/>\n    if &#034;error&#034; in data:<br \/>\n        return f&#034;\u26a0\ufe0f {data[&#039;error&#039;]}&#034;<\/p>\n<p>    # \u63d0\u53d6\u6570\u636e\u65f6\u505a\u5bb9\u9519\u5904\u7406<br \/>\n    city &#061; data.get(&#034;name&#034;, &#034;\u672a\u77e5&#034;)<br \/>\n    country &#061; data.get(&#034;sys&#034;, {}).get(&#034;country&#034;, &#034;\u672a\u77e5&#034;)<br \/>\n    temp &#061; data.get(&#034;main&#034;, {}).get(&#034;temp&#034;, &#034;N\/A&#034;)<br \/>\n    humidity &#061; data.get(&#034;main&#034;, {}).get(&#034;humidity&#034;, &#034;N\/A&#034;)<br \/>\n    wind_speed &#061; data.get(&#034;wind&#034;, {}).get(&#034;speed&#034;, &#034;N\/A&#034;)<br \/>\n    # weather \u53ef\u80fd\u4e3a\u7a7a\u5217\u8868&#xff0c;\u56e0\u6b64\u7528 [0] \u524d\u5148\u63d0\u4f9b\u9ed8\u8ba4\u5b57\u5178<br \/>\n    weather_list &#061; data.get(&#034;weather&#034;, [{}])<br \/>\n    description &#061; weather_list[0].get(&#034;description&#034;, &#034;\u672a\u77e5&#034;)<\/p>\n<p>    return (<br \/>\n        f&#034;&#x1f30d; {city}, {country}\\\\n&#034;<br \/>\n        f&#034;&#x1f321; \u6e29\u5ea6: {temp}\u00b0C\\\\n&#034;<br \/>\n        f&#034;&#x1f4a7; \u6e7f\u5ea6: {humidity}%\\\\n&#034;<br \/>\n        f&#034;&#x1f32c; \u98ce\u901f: {wind_speed} m\/s\\\\n&#034;<br \/>\n        f&#034;&#x1f324; \u5929\u6c14: {description}\\\\n&#034;<br \/>\n    )<\/p>\n<p>&#064;mcp.tool()<br \/>\nasync def query_weather(city: str) -&gt; str:<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u8f93\u5165\u6307\u5b9a\u57ce\u5e02\u7684\u82f1\u6587\u540d\u79f0&#xff0c;\u8fd4\u56de\u4eca\u65e5\u5929\u6c14\u67e5\u8be2\u7ed3\u679c\u3002<br \/>\n    :param city: \u57ce\u5e02\u540d\u79f0&#xff08;\u9700\u4f7f\u7528\u82f1\u6587&#xff09;<br \/>\n    :return: \u683c\u5f0f\u5316\u540e\u7684\u5929\u6c14\u4fe1\u606f<br \/>\n    &#034;&#034;&#034;<br \/>\n    data &#061; await fetch_weather(city)<br \/>\n    return format_weather(data)<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    # \u4ee5\u6807\u51c6 I\/O \u65b9\u5f0f\u8fd0\u884c MCP \u670d\u52a1\u5668<br \/>\n    mcp.run(transport&#061;&#039;stdio&#039;) <\/p>\n<p>\u4ee3\u7801\u89e3\u91ca\u5982\u4e0b&#xff1a;<\/p>\n<p>Part 1. \u5f02\u6b65\u83b7\u53d6\u5929\u6c14\u6570\u636e<\/p>\n<ul>\n<li>\n<p>\u51fd\u6570 fetch_weather(city: str)<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 httpx.AsyncClient() \u53d1\u9001\u5f02\u6b65 GET \u8bf7\u6c42\u5230 OpenWeather API\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u8bf7\u6c42\u6210\u529f&#xff0c;\u5219\u8c03\u7528 response.json() \u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/li>\n<li>\n<p>\u51fa\u73b0\u5f02\u5e38\u65f6&#xff0c;\u8fd4\u56de\u5305\u542b\u9519\u8bef\u4fe1\u606f\u7684\u5b57\u5178\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Part 2. \u683c\u5f0f\u5316\u5929\u6c14\u6570\u636e<\/p>\n<ul>\n<li>\n<p>\u51fd\u6570 format_weather(data: dict | str)<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\u68c0\u67e5\u4f20\u5165\u7684\u6570\u636e\u662f\u5426\u4e3a\u5b57\u7b26\u4e32&#xff0c;\u5982\u679c\u662f&#xff0c;\u5219\u4f7f\u7528 json.loads \u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u6570\u636e\u4e2d\u662f\u5426\u5305\u542b &#034;error&#034; \u5b57\u6bb5&#xff0c;\u5982\u679c\u6709&#xff0c;\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 .get() \u65b9\u6cd5\u63d0\u53d6 name\u3001sys.country\u3001main.temp\u3001main.humidity\u3001wind.speed \u548c weather[0].description \u7b49\u6570\u636e&#xff0c;\u5e76\u4e3a\u53ef\u80fd\u7f3a\u5931\u7684\u5b57\u6bb5\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c06\u63d0\u53d6\u7684\u4fe1\u606f\u62fc\u63a5\u6210\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32&#xff0c;\u65b9\u4fbf\u9605\u8bfb\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Part 3. MCP \u5de5\u5177 query_weather(city: str)<\/p>\n<ul>\n<li>\n<p>\u51fd\u6570 query_weather<\/p>\n<ul>\n<li>\n<p>\u901a\u8fc7 &#064;mcp.tool() \u88c5\u9970\u5668\u6ce8\u518c\u4e3a MCP \u670d\u52a1\u5668\u7684\u5de5\u5177&#xff0c;\u4f7f\u5176\u80fd\u591f\u88ab\u5ba2\u6237\u7aef\u8c03\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528 fetch_weather(city) \u83b7\u53d6\u5929\u6c14\u6570\u636e&#xff0c;\u7136\u540e\u7528 format_weather(data) \u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6613\u8bfb\u6587\u672c&#xff0c;\u6700\u540e\u8fd4\u56de\u8be5\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Part 4. \u8fd0\u884c\u670d\u52a1\u5668<\/p>\n<ul>\n<li>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;: \u5757<\/p>\n<ul>\n<li>\n<p>\u8c03\u7528 mcp.run(transport&#061;&#039;stdio&#039;) \u542f\u52a8 MCP \u670d\u52a1\u5668&#xff0c;\u91c7\u7528\u6807\u51c6 I\/O \u901a\u4fe1\u65b9\u5f0f&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8c03\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6b64\u5916&#xff0c;\u4e0a\u8ff0\u4ee3\u7801\u6709\u4e24\u4e2a\u6ce8\u610f\u4e8b\u9879&#xff0c;<\/p>\n<li>\n<p>query_weather\u51fd\u6570\u7684\u51fd\u6570\u8bf4\u660e\u81f3\u5173\u91cd\u8981&#xff0c;\u76f8\u5f53\u4e8e\u662f\u6b64\u540e\u5ba2\u6237\u7aef\u5bf9\u51fd\u6570\u8fdb\u884c\u8bc6\u522b\u7684\u57fa\u672c\u4f9d\u636e&#xff0c;\u56e0\u6b64\u9700\u8981\u8c28\u614e\u7f16\u5199&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5f53\u6307\u5b9a transport&#061;&#039;stdio&#039; \u8fd0\u884c MCP \u670d\u52a1\u5668\u65f6&#xff0c;\u5ba2\u6237\u7aef\u5fc5\u987b\u5728\u542f\u52a8\u65f6\u540c\u65f6\u542f\u52a8\u5f53\u524d\u8fd9\u4e2a\u811a\u672c&#xff0c;\u5426\u5219\u65e0\u6cd5\u987a\u5229\u901a\u4fe1\u3002\u8fd9\u662f\u56e0\u4e3a stdio \u6a21\u5f0f\u662f\u4e00\u79cd\u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;IPC&#xff0c;Inter-Process Communication&#xff09;\u65b9\u5f0f&#xff0c;\u5b83\u9700\u8981\u670d\u52a1\u5668\u4f5c\u4e3a\u5b50\u8fdb\u7a0b\u8fd0\u884c&#xff0c;\u5e76\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u8f93\u51fa&#xff08;stdin\/stdout&#xff09;\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/li>\n<p>\u56e0\u6b64&#xff0c;\u5f53\u6211\u4eec\u7f16\u5199\u5b8c\u670d\u52a1\u5668\u540e&#xff0c;\u5e76\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u8fd9\u4e2a\u670d\u52a1\u5668&#xff0c;\u800c\u662f\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684\u80fd\u591f\u8fdb\u884cstdio\u7684\u5ba2\u6237\u7aef&#xff0c;\u624d\u80fd\u987a\u5229\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf28\u6b21\u3002MCP\u670d\u52a1\u5668\u662f\u57fa\u4e8eModelContextProtocol\uff08MCP\uff09\u534f\u8bae\u7684\u670d\u52a1\u5668\u3002MCP\u534f\u8bae\u652f\u6301\u6807\u51c6\u8f93\u5165\u8f93\u51fa\uff08stdio\uff09\u548c\u57fa\u4e8eHTTP\u7684\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e24\u79cd\u4f20\u8f93\u65b9\u5f0f\uff0c\u8fd1\u671f\u6709\u63d0\u6848\u5efa\u8bae\u91c7\u7528\u201c\u53ef\u6d41\u5f0f\u4f20\u8f93\u7684HTTP\u201d\u66ff\u4ee3\u73b0\u6709\u65b9\u6848\u4ee5\u4f18\u5316\u8fdc\u7a0b\u4f20\u8f93\u3002\u672c\u6587\u4ee5\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7MCP\u534f\u8bae\u521b\u5efa\u670d\u52a1\u5668\uff0c\u5305\u62ec\u4f9d\u8d56\u5b89\u88c5\u3001\u4ee3\u7801\u7f16\u5199\u53ca\u8fd0\u884c\u6d41\u7a0b\u3002\u670d\u52a1\u5668\u901a\u8fc7OpenWeatherAPI\u83b7\u53d6\u5929\u6c14\u6570\u636e\uff0c\u5e76\u4f7f\u7528MCP\u5de5\u5177\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6613\u8bfb\u6587\u672c\u3002\u670d\u52a1\u5668\u4ee5\u6807\u51c6I\/O\u65b9\u5f0f\u8fd0\u884c\uff0c\u9700\u4e0e\u5ba2\u6237\u7aef\u540c\u65f6\u542f\u52a8\u4ee5\u5b9e\u73b0\u901a\u4fe1\u3002<\/p>\n","protected":false},"author":2,"featured_media":39945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[50,43],"topic":[],"class_list":["post-39949","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-50","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/39949.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf28\u6b21\u3002MCP\u670d\u52a1\u5668\u662f\u57fa\u4e8eModelContextProtocol\uff08MCP\uff09\u534f\u8bae\u7684\u670d\u52a1\u5668\u3002MCP\u534f\u8bae\u652f\u6301\u6807\u51c6\u8f93\u5165\u8f93\u51fa\uff08stdio\uff09\u548c\u57fa\u4e8eHTTP\u7684\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e24\u79cd\u4f20\u8f93\u65b9\u5f0f\uff0c\u8fd1\u671f\u6709\u63d0\u6848\u5efa\u8bae\u91c7\u7528\u201c\u53ef\u6d41\u5f0f\u4f20\u8f93\u7684HTTP\u201d\u66ff\u4ee3\u73b0\u6709\u65b9\u6848\u4ee5\u4f18\u5316\u8fdc\u7a0b\u4f20\u8f93\u3002\u672c\u6587\u4ee5\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7MCP\u534f\u8bae\u521b\u5efa\u670d\u52a1\u5668\uff0c\u5305\u62ec\u4f9d\u8d56\u5b89\u88c5\u3001\u4ee3\u7801\u7f16\u5199\u53ca\u8fd0\u884c\u6d41\u7a0b\u3002\u670d\u52a1\u5668\u901a\u8fc7OpenWeatherAPI\u83b7\u53d6\u5929\u6c14\u6570\u636e\uff0c\u5e76\u4f7f\u7528MCP\u5de5\u5177\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6613\u8bfb\u6587\u672c\u3002\u670d\u52a1\u5668\u4ee5\u6807\u51c6I\/O\u65b9\u5f0f\u8fd0\u884c\uff0c\u9700\u4e0e\u5ba2\u6237\u7aef\u540c\u65f6\u542f\u52a8\u4ee5\u5b9e\u73b0\u901a\u4fe1\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/39949.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-26T08:11:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250526081117-68342225e8aa2.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39949.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/39949.html\",\"name\":\"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-26T08:11:20+00:00\",\"dateModified\":\"2025-05-26T08:11:20+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/39949.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/39949.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39949.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/39949.html","og_locale":"zh_CN","og_type":"article","og_title":"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf28\u6b21\u3002MCP\u670d\u52a1\u5668\u662f\u57fa\u4e8eModelContextProtocol\uff08MCP\uff09\u534f\u8bae\u7684\u670d\u52a1\u5668\u3002MCP\u534f\u8bae\u652f\u6301\u6807\u51c6\u8f93\u5165\u8f93\u51fa\uff08stdio\uff09\u548c\u57fa\u4e8eHTTP\u7684\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e24\u79cd\u4f20\u8f93\u65b9\u5f0f\uff0c\u8fd1\u671f\u6709\u63d0\u6848\u5efa\u8bae\u91c7\u7528\u201c\u53ef\u6d41\u5f0f\u4f20\u8f93\u7684HTTP\u201d\u66ff\u4ee3\u73b0\u6709\u65b9\u6848\u4ee5\u4f18\u5316\u8fdc\u7a0b\u4f20\u8f93\u3002\u672c\u6587\u4ee5\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7MCP\u534f\u8bae\u521b\u5efa\u670d\u52a1\u5668\uff0c\u5305\u62ec\u4f9d\u8d56\u5b89\u88c5\u3001\u4ee3\u7801\u7f16\u5199\u53ca\u8fd0\u884c\u6d41\u7a0b\u3002\u670d\u52a1\u5668\u901a\u8fc7OpenWeatherAPI\u83b7\u53d6\u5929\u6c14\u6570\u636e\uff0c\u5e76\u4f7f\u7528MCP\u5de5\u5177\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6613\u8bfb\u6587\u672c\u3002\u670d\u52a1\u5668\u4ee5\u6807\u51c6I\/O\u65b9\u5f0f\u8fd0\u884c\uff0c\u9700\u4e0e\u5ba2\u6237\u7aef\u540c\u65f6\u542f\u52a8\u4ee5\u5b9e\u73b0\u901a\u4fe1\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/39949.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-26T08:11:20+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250526081117-68342225e8aa2.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/39949.html","url":"https:\/\/www.wsisp.com\/helps\/39949.html","name":"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-26T08:11:20+00:00","dateModified":"2025-05-26T08:11:20+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/39949.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/39949.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/39949.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"MCP\u5929\u6c14\u67e5\u8be2\u670d\u52a1\u5668server\u4e0e\u4f7f\u7528"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=39949"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39949\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/39945"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=39949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=39949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=39949"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=39949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}