{"id":5786,"date":"2026-03-01T11:14:07","date_gmt":"2026-03-01T03:14:07","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5786"},"modified":"2026-03-03T14:33:39","modified_gmt":"2026-03-03T06:33:39","slug":"9-mcp-%e6%8f%90%e7%a4%ba%e8%af%8d%e5%b7%a5%e7%a8%8b-%e6%95%99%e5%ad%a6%e4%b8%8e%e6%a8%a1%e6%9d%bf%e6%8a%80%e6%9c%af%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5786","title":{"rendered":"9.MCP-\u63d0\u793a\u8bcd\u5de5\u7a0b-\u6559\u5b66\u4e0e\u6a21\u677f\u6280\u672f\u5b9e\u73b0"},"content":{"rendered":"<p>MCP\u4e4b\u4e2d\u7684\u63d0\u793a\u8bcd\u5de5\u7a0b<\/p>\n<p>\u5728MCP\u4e4b\u4e2d\uff0c\u63d0\u793a\u8bcdPrompt\u548c\u8d44\u6e90Resource\uff0c\u5de5\u5177Tool \u4e00\u6837\u91cd\u8981\uff0c\u53ef\u4ee5\u4e3a\u5ba2\u6237\u7aef\u63d0\u4f9b\u4e00\u4e2a\u6807\u51c6\u5316\u7684\uff0c\u7ed3\u6784\u5316\u7684\uff0c\u53ef\u4ee5\u590d\u7528\u7684\u5bf9\u8bdd\u6a21\u677f\u3002\u4ece\u800c\u505a\u5230\u5728\u7528\u6237\u7ed9\u51fa\u5c11\u91cf\u63d0\u793a\u4e4b\u540e\uff0c\u5229\u7528\u5927\u6a21\u578b\u5f97\u5230\u6ee1\u610f\u7684\u7b54\u590d\u3002<\/p>\n<p>\u800c\u63d0\u793a\u8bcd\u5bf9\u4e8e\u5927\u6a21\u578b\u7684\u91cd\u8981\u4e0d\u8a00\u800c\u55bb\uff0c\u4e5f\u662f\u4e1a\u754c\u4e00\u76f4\u5f3a\u8c03\u7684\u5185\u5bb9\u3002\u8bf8\u5982\u901a\u8fc7Few shot example\u6765\u589e\u5f3a\u5927\u6a21\u578b\u4e0a\u4e0b\u6587\u5bf9\u8bdd\u80fd\u529b\u3002\u8bf8\u5982\u5404\u7c7b\u63d0\u793a\u5de5\u7a0b\u6765\u589e\u5f3a\u5927\u6a21\u578b\u7684\u601d\u7ef4\u80fd\u529b\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4e3e\u4e00\u4e2a\u63d0\u793a\u8bcd\u548c\u9886\u57df\u77e5\u8bc6\u7ed3\u5408\u7684\u4f8b\u5b50<\/p>\n<p>\u6bd4\u5982\u5bf9\u4e8e\u6559\u80b2\u754c\u6765\u8bf4\uff0c\u6559\u5e08\u5728\u8fdb\u884c\u6559\u5b66\u7684\u65f6\u5019\uff0c\u5f80\u5f80\u9762\u5bf9\u7684\u95ee\u9898\u4e0d\u662f\u6ca1\u6709\u8d44\u6e90\uff0c\u800c\u662f\u5982\u4f55\u5229\u7528\u8d44\u6e90\uff0c\u8fd9\u4e5f\u662f\u63d0\u793a\u5de5\u7a0b\u64c5\u957f\u7684\u90e8\u5206\u3002<\/p>\n<p>\u6bd4\u5982\u5c06\u4f20\u7edf\u7684\u6559\u5b66\uff0c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5177\u5907 \u53cd\u601d-\u5f15\u5bfc-\u751f\u6210-\u53cd\u9988\u7684\u63d0\u793a\u4f53\u7cfb\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1617\" height=\"655\" class=\"wp-image-5787\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-18.png\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-18.png 1617w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-18-300x122.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-18-1024x415.png 1024w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-18-768x311.png 768w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-18-1536x622.png 1536w\" sizes=\"(max-width: 1617px) 100vw, 1617px\" \/><\/p>\n<p>\u8fd9\u91cc\u7684\u65b9\u6cd5\u672c\u8d28\uff0c\u5c31\u662f\u5c06\u6559\u80b2\u573a\u666f\u7684\u6559\u5b66\u601d\u7ef4\u5bfc\u56fe\uff0c\u7ed3\u6784\u5316\u7684\u8868\u8fbe\u4e3a\u63d0\u793a\u8bcd\u6a21\u677f<\/p>\n<p>\u901a\u8fc7\u7528\u6237\u7279\u5b9a\u7684\u8f93\u5165\uff0c\u6bd4\u5982\u8bfe\u672c\u4e0a\u7684\u4e00\u4e2a\u7ae0\u8282\uff0c\u7ed3\u5408\u7279\u5b9a\u7684Prompt\u3002\u6700\u7ec8\u8f93\u51fa\u4e00\u4e2a\u5408\u683c\u7684\u6559\u6848\u3002<\/p>\n<p>\u90a3\u4e48\u5230MCP\u4e4b\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5176\u5185\u7f6e\u8fdb\u53bb\uff0c\u901a\u8fc7\u4e00\u4e2a\u5408\u9002\u7684\u9884\u7f6e\u63d0\u793a\u8bcd\uff0c\u8ba9\u5927\u6a21\u578b\u4f9d\u6b21\u4f7f\u7528\u8fd9\u4e9b\u63d0\u793a\u8bcd\uff0c\u6700\u7ec8\u5f62\u6210\u4e00\u4e2a\u6559\u6848\u3002<\/p>\n<p>\u9664\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728MCP HOST\u4e4b\u4e2d\u5b9a\u4e49\u4e00\u7cfb\u5217\u7684\u83dc\u5355\u6216\u8005\u6309\u94ae\uff0c\u6765\u89e6\u53d1LLM\u4efb\u52a1\u3002<\/p>\n<p>\u56e0\u6b64\u5728MCP\u4e4b\u4e2d\uff0c\u5177\u4f53\u7684\u8bbe\u8ba1\u6d41\u7a0b\u5c31\u662f\u5728\u670d\u52a1\u5668\u7aef\u63d0\u524d\u5b9a\u4e49\u597d\u4e00\u5957Prompt\u6a21\u677f\uff0c\u901a\u8fc7MCP\u66b4\u9732\u7ed9\u5ba2\u6237\uff0c\u652f\u6301\u7528\u6237\u6216\u8005\u4ea7\u54c1\u4ee5\u4e00\u81f4\u7684\u65b9\u5f0f\u8fdb\u884c\u9009\u586b\u5e76\u8c03\u7528\u3002<\/p>\n<p>MCP\u4e4b\u4e2d\u7684\u63d0\u793a\u8bcd\u5177\u6709\u4e00\u7cfb\u5217\u7684\u7279\u6027\uff0c\u8bf8\u5982 \u53c2\u6570\u5316\uff0c\u652f\u6301Client\u8f93\u5165\u4e00\u7cfb\u5217\u7684\u53c2\u6570\u6765\u52a8\u6001\u751f\u6210\u3002 \u8d44\u6e90\u6574\u5408\uff0c\u53ef\u4ee5\u5d4c\u5165\u4e00\u7cfb\u5217\u7684\u4e0a\u4e0b\u6587\u3002\u7edf\u4e00\u53d1\u73b0\uff0c\u901a\u8fc7prompts\/list\u548cprompts\/get\u63a5\u53e3\u6765\u7edf\u4e00\u8c03\u7528\u548c\u6ce8\u518c\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u770b\u4e0bMCP\u4e2d\u6a21\u677f\u7684\u5177\u4f53\u6280\u672f\u5b9e\u73b0\u3002<\/p>\n<p>\u6bcf\u4e2a\u63d0\u793a\u4e2d\u5305\u542b\u4e00\u7cfb\u5217\u7684\u5b57\u6bb5\uff0c\u8bf8\u5982 name\uff0c description\uff0c arguments<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>name: string; \/\/ \u63d0\u793a\u7684\u552f\u4e00\u6807\u8bc6\u7b26<\/p>\n<p>description?: string; \/\/ \u53ef\u9009\u7684\u4eba\u7c7b\u53ef\u8bfb\u63cf\u8ff0<\/p>\n<p>arguments?: [ \/\/ \u53ef\u9009\u53c2\u6570\u5217\u8868<\/p>\n<p>{<\/p>\n<p>name: string;<\/p>\n<p>description?: string;<\/p>\n<p>required?: boolean;<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u6a21\u677f\u793a\u4f8b<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;name&#8221;: &#8220;explain-code&#8221;,<\/p>\n<p>&#8220;description&#8221;: &#8220;Explain how a code snippet works&#8221;,<\/p>\n<p>&#8220;arguments&#8221;: [<\/p>\n<p>{<\/p>\n<p>&#8220;name&#8221;: &#8220;code&#8221;,<\/p>\n<p>&#8220;description&#8221;: &#8220;The code to explain&#8221;,<\/p>\n<p>&#8220;required&#8221;: true<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>&#8220;name&#8221;: &#8220;language&#8221;,<\/p>\n<p>&#8220;description&#8221;: &#8220;The programming language&#8221;,<\/p>\n<p>&#8220;required&#8221;: false<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u57fa\u4e8e\u8fd9\u4e2a\u63d0\u793a\u6a21\u677f\uff0c\u5ba2\u6237\u7aef\u5728\u8c03\u7528\u7684\u65f6\u5019\u7684\u8bf7\u6c42\u4e3a<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;jsonrpc&#8221;: &#8220;2.0&#8221;,<\/p>\n<p>&#8220;id&#8221;: 1,<\/p>\n<p>&#8220;method&#8221;: &#8220;prompts\/get&#8221;,<\/p>\n<p>&#8220;params&#8221;: {<\/p>\n<p>&#8220;name&#8221;: &#8220;explain-code&#8221;,<\/p>\n<p>&#8220;arguments&#8221;: {<\/p>\n<p>&#8220;code&#8221;: &#8220;def foo(): return 42&#8221;,<\/p>\n<p>&#8220;language&#8221;: &#8220;python&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e76\u4e14\u53ef\u4ee5\u83b7\u53d6\u5230\u7684\u54cd\u5e94\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;jsonrpc&#8221;: &#8220;2.0&#8221;,<\/p>\n<p>&#8220;id&#8221;: 1,<\/p>\n<p>&#8220;messages&#8221;: [<\/p>\n<p>{<\/p>\n<p>&#8220;role&#8221;: &#8220;user&#8221;,<\/p>\n<p>&#8220;content&#8221;: {<\/p>\n<p>&#8220;type&#8221;: &#8220;text&#8221;,<\/p>\n<p>&#8220;text&#8221;: &#8220;Explain how this python code works:\\n\\ndef foo(): return 42&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9664\u6b64\u5916\uff0cprompt\u4e5f\u652f\u6301\u4e86listChanged\u76f8\u5173\u63a5\u53e3\uff0c\u5176\u7528\u6cd5\u548cresource\u4e00\u6837\u3002<\/p>\n<p>\u5e76\u4e14\u5728MCP\u4e4b\u4e2d\uff0c\u4e5f\u652f\u6301\u4e0d\u540c\u7c7b\u578b\u7684\u6d88\u606f\u3002<\/p>\n<p>Content\u4e4b\u4e2d\uff0c\u5305\u542b\u6587\u672c\u5185\u5bb9<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;type&#8221;: &#8220;text&#8221;,<\/p>\n<p>&#8220;text&#8221;: &#8220;The text content of the message&#8221;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56fe\u50cf\u5185\u5bb9<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;type&#8221;: &#8220;image&#8221;,<\/p>\n<p>&#8220;data&#8221;: &#8220;base64-encoded-image-data&#8221;,<\/p>\n<p>&#8220;mimeType&#8221;: &#8220;image\/png&#8221;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u97f3\u9891\u5185\u5bb9<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;type&#8221;: &#8220;audio&#8221;,<\/p>\n<p>&#8220;data&#8221;: &#8220;base64-encoded-audio-data&#8221;,<\/p>\n<p>&#8220;mimeType&#8221;: &#8220;audio\/wav&#8221;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5d4c\u5165\u5f0f\u8d44\u6e90<\/p>\n<table>\n<tbody>\n<tr>\n<td>{<\/p>\n<p>&#8220;type&#8221;: &#8220;resource&#8221;,<\/p>\n<p>&#8220;resource&#8221;: {<\/p>\n<p>&#8220;uri&#8221;: &#8220;resource:\/\/example&#8221;,<\/p>\n<p>&#8220;mimeType&#8221;: &#8220;text\/plain&#8221;,<\/p>\n<p>&#8220;text&#8221;: &#8220;Resource content&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u8fd8\u4f1a\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684Demo\uff0c\u6765\u5b9e\u73b0MCP\u6a21\u677f\u751f\u6210\u7684\u5b8c\u6574\u6d41\u7a0b<\/p>\n<p>\u9996\u5148\u9700\u8981\u6211\u4eec\u5b9e\u73b0\u670d\u52a1\u5668\u7aef\u7684\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u6a21\u677f\uff0c\u65b9\u4fbfclient\u901a\u8fc7list\/prompt\u6765\u67e5\u770b\u6709\u54ea\u4e9b\u6a21\u677f\u5e76\u8fd4\u56de\u3002<\/p>\n<p>\u4e24\u4e2a\u6a21\u677f\u5206\u522b\u662fcode-review\u548cexplain-code<\/p>\n<p>\u4e00\u4e2a\u662f\u8ba9\u5927\u6a21\u578b\u6765\u5ba1\u67e5\u5bf9\u5e94\u7684\u4ee3\u7801\uff1b\u53e6\u4e00\u4e2a\u5219\u662f\u53bb\u89e3\u91ca\u4e00\u6bb5\u4ee3\u7801\u7684\u4f5c\u7528\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>PROMPTS = {<\/p>\n<p>&#8220;code-review&#8221;: types.Prompt(<\/p>\n<p>name=&#8221;code-review&#8221;,<\/p>\n<p>description=&#8221;\u5206\u6790\u4ee3\u7801\u5e76\u63d0\u4f9b\u6539\u8fdb\u5efa\u8bae&#8221;,<\/p>\n<p>arguments=[<\/p>\n<p>types.PromptArgument(<\/p>\n<p>name=&#8221;code&#8221;,<\/p>\n<p>description=&#8221;\u9700\u8981\u5ba1\u67e5\u7684\u4ee3\u7801&#8221;,<\/p>\n<p>required=True<\/p>\n<p>),<\/p>\n<p>types.PromptArgument(<\/p>\n<p>name=&#8221;language&#8221;,<\/p>\n<p>description=&#8221;\u7f16\u7a0b\u8bed\u8a00&#8221;,<\/p>\n<p>required=True<\/p>\n<p>),<\/p>\n<p>types.PromptArgument(<\/p>\n<p>name=&#8221;focus&#8221;,<\/p>\n<p>description=&#8221;\u5ba1\u67e5\u91cd\u70b9\uff08\u53ef\u9009\uff1aperformance, security, readability\uff09&#8221;,<\/p>\n<p>required=False<\/p>\n<p>)<\/p>\n<p>]<\/p>\n<p>),<\/p>\n<p>&#8220;explain-code&#8221;: types.Prompt(<\/p>\n<p>name=&#8221;explain-code&#8221;,<\/p>\n<p>description=&#8221;\u89e3\u91ca\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406&#8221;,<\/p>\n<p>arguments=[<\/p>\n<p>types.PromptArgument(<\/p>\n<p>name=&#8221;code&#8221;,<\/p>\n<p>description=&#8221;\u9700\u8981\u89e3\u91ca\u7684\u4ee3\u7801&#8221;,<\/p>\n<p>required=True<\/p>\n<p>),<\/p>\n<p>types.PromptArgument(<\/p>\n<p>name=&#8221;language&#8221;,<\/p>\n<p>description=&#8221;\u7f16\u7a0b\u8bed\u8a00&#8221;,<\/p>\n<p>required=True<\/p>\n<p>)<\/p>\n<p>]<\/p>\n<p>)<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e00\u6bb5\u4ee3\u7801\u4e4b\u4e2d\u4e3b\u8981\u662f\u58f0\u660e\u4e24\u4e2aprompt\u7684\u76f8\u5173\u5143\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<p>\u4e4b\u540e\u662f\u5229\u7528@mcp.prompts_list()\u4ee5\u53ca@mcp.prompts_get\u6765\u6ce8\u518c\u63d0\u793a\u6a21\u677f<\/p>\n<table>\n<tbody>\n<tr>\n<td>@app.list_prompts()<\/p>\n<p>async def list_prompts() -&gt; list[types.Prompt]:<\/p>\n<p>&#8220;&#8221;&#8221;\u8fd4\u56de\u53ef\u7528\u7684\u63d0\u793a\u6a21\u677f\u5217\u8868&#8221;&#8221;&#8221;<\/p>\n<p>return list(PROMPTS.values())<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5bf9\u5e94\u7684\u83b7\u53d6\u6a21\u677f\u5219\u662f<\/p>\n<table>\n<tbody>\n<tr>\n<td>@app.get_prompt()<\/p>\n<p>async def get_prompt(<\/p>\n<p>name: str, arguments: dict[str, str] | None = None<\/p>\n<p>) -&gt; types.GetPromptResult:<\/p>\n<p>&#8220;&#8221;&#8221;\u6839\u636e\u540d\u79f0\u548c\u53c2\u6570\u83b7\u53d6\u63d0\u793a\u5185\u5bb9&#8221;&#8221;&#8221;<\/p>\n<p>if name not in PROMPTS:<\/p>\n<p>raise ValueError(f&#8221;\u63d0\u793a\u6a21\u677f &#8216;{name}&#8217; \u4e0d\u5b58\u5728&#8221;)<\/p>\n<p>if name == &#8220;code-review&#8221;:<\/p>\n<p>code = arguments.get(&#8220;code&#8221;) if arguments else &#8220;&#8221;<\/p>\n<p>language = arguments.get(&#8220;language&#8221;) if arguments else &#8220;Unknown&#8221;<\/p>\n<p>focus = arguments.get(&#8220;focus&#8221;, &#8220;general&#8221;) if arguments else &#8220;general&#8221;<\/p>\n<p>return types.GetPromptResult(<\/p>\n<p>messages=[<\/p>\n<p>types.PromptMessage(<\/p>\n<p>role=&#8221;system&#8221;,<\/p>\n<p>content=types.TextContent(<\/p>\n<p>type=&#8221;text&#8221;,<\/p>\n<p>text=f&#8221;\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u4ee3\u7801\u5ba1\u67e5\u52a9\u624b\uff0c\u4e13\u6ce8\u4e8e{language}\u4ee3\u7801\u7684{focus}\u65b9\u9762\u3002&#8221;<\/p>\n<p>)<\/p>\n<p>),<\/p>\n<p>types.PromptMessage(<\/p>\n<p>role=&#8221;user&#8221;,<\/p>\n<p>content=types.TextContent(<\/p>\n<p>type=&#8221;text&#8221;,<\/p>\n<p>text=f&#8221;\u8bf7\u5ba1\u67e5\u4ee5\u4e0b{language}\u4ee3\u7801\uff0c\u5e76\u63d0\u4f9b\u6539\u8fdb\u5efa\u8bae\uff1a\\n\\n{code}&#8221;<\/p>\n<p>)<\/p>\n<p>)<\/p>\n<p>]<\/p>\n<p>)<\/p>\n<p>elif name == &#8220;explain-code&#8221;:<\/p>\n<p>code = arguments.get(&#8220;code&#8221;) if arguments else &#8220;&#8221;<\/p>\n<p>language = arguments.get(&#8220;language&#8221;) if arguments else &#8220;Unknown&#8221;<\/p>\n<p>return types.GetPromptResult(<\/p>\n<p>messages=[<\/p>\n<p>types.PromptMessage(<\/p>\n<p>role=&#8221;assistant&#8221;,<\/p>\n<p>content=types.TextContent(<\/p>\n<p>type=&#8221;text&#8221;,<\/p>\n<p>text=f&#8221;\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u7f16\u7a0b\u5bfc\u5e08\uff0c\u64c5\u957f\u89e3\u91ca{language}\u4ee3\u7801\u3002&#8221;<\/p>\n<p>)<\/p>\n<p>),<\/p>\n<p>types.PromptMessage(<\/p>\n<p>role=&#8221;user&#8221;,<\/p>\n<p>content=types.TextContent(<\/p>\n<p>type=&#8221;text&#8221;,<\/p>\n<p>text=f&#8221;\u8bf7\u89e3\u91ca\u4ee5\u4e0b{language}\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\uff1a\\n\\n{code}&#8221;<\/p>\n<p>)<\/p>\n<p>)<\/p>\n<p>]<\/p>\n<p>)<\/p>\n<p>raise ValueError(f&#8221;\u672a\u5b9e\u73b0\u63d0\u793a\u6a21\u677f &#8216;{name}&#8217; \u7684\u5904\u7406\u903b\u8f91&#8221;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u6839\u636e\u7528\u6237\u4f20\u5165\u7684argument\uff0c\u6765\u5bf9Prompt\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<p>\u5e76\u8fd4\u56deGetPromptResult\u7c7b\u578b\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5bf9\u4e8e\u5ba2\u6237\u7aef\u6765\u8bf4\uff0c\u6211\u4eec\u4f1a\u901a\u8fc7stdio\u65b9\u5f0f\u8fde\u63a5client\uff0c\u5e76\u901a\u8fc7\u5bf9\u5e94\u7684list_prompts()\u51fd\u6570\u6765\u8fdb\u884c\u83b7\u53d6\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>class CodeReviewClient:<\/p>\n<p>def __init__(self):<\/p>\n<p>self.session = None<\/p>\n<p>self.transport = None<\/p>\n<p>self.client = OpenAI(<\/p>\n<p>api_key=os.getenv(&#8220;DEEPSEEK_API_KEY&#8221;),<\/p>\n<p>base_url=&#8221;https:\/\/api.deepseek.com&#8221;<\/p>\n<p>)<\/p>\n<p>self.prompts = None<\/p>\n<p>async def connect(self, server_script: str):<\/p>\n<p># 1) \u6784\u9020\u53c2\u6570\u5bf9\u8c61<\/p>\n<p>params = StdioServerParameters(<\/p>\n<p>command=&#8221;\/mnt\/external_disk\/venv\/20250426_MCP_Server\/bin\/python&#8221;,<\/p>\n<p>args=[server_script],<\/p>\n<p>cwd=&#8221;..\/server&#8221; # \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u4e3a\u670d\u52a1\u5668\u76ee\u5f55<\/p>\n<p>)<\/p>\n<p># 2) \u4fdd\u5b58\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/p>\n<p>self.transport = stdio_client(params)<\/p>\n<p># 3) \u8fdb\u5165\u4e0a\u4e0b\u6587\uff0c\u62ff\u5230 stdio, write<\/p>\n<p>self.stdio, self.write = await self.transport.__aenter__()<\/p>\n<p># 4) \u521d\u59cb\u5316 MCP \u4f1a\u8bdd<\/p>\n<p>self.session = await ClientSession(self.stdio, self.write).__aenter__()<\/p>\n<p>await self.session.initialize()<\/p>\n<p># 5) \u83b7\u53d6\u53ef\u7528\u7684\u63d0\u793a\u6a21\u677f<\/p>\n<p>self.prompts = await self.session.list_prompts()<\/p>\n<p># \u517c\u5bb9 dict_items<\/p>\n<p>if not isinstance(self.prompts, dict):<\/p>\n<p>self.prompts = dict(self.prompts)<\/p>\n<p>prompt_list = self.prompts.get(&#8220;prompts&#8221;, [])<\/p>\n<p>print(&#8220;\u53ef\u7528\u63d0\u793a\u6a21\u677f\uff1a&#8221;)<\/p>\n<p>for prompt in prompt_list:<\/p>\n<p>if hasattr(prompt, &#8216;name&#8217;) and hasattr(prompt, &#8216;description&#8217;):<\/p>\n<p>print(f&#8221;- {prompt.name}: {prompt.description}&#8221;)<\/p>\n<p>else:<\/p>\n<p>print(f&#8221;- \u672a\u77e5\u63d0\u793a\u6a21\u677f: {prompt}&#8221;)<\/p>\n<p>async def use_prompt(self, prompt_name: str, arguments: dict[str, str]):<\/p>\n<p># \u83b7\u53d6\u63d0\u793a\u5185\u5bb9<\/p>\n<p>prompt_result = await self.session.get_prompt(prompt_name, arguments)<\/p>\n<p># \u8f6c\u6362\u6d88\u606f\u683c\u5f0f\u4e3a OpenAI API \u6240\u9700\u7684\u683c\u5f0f<\/p>\n<p>messages = []<\/p>\n<p>for msg in prompt_result.messages:<\/p>\n<p>if isinstance(msg.content, types.TextContent):<\/p>\n<p>messages.append({<\/p>\n<p>&#8220;role&#8221;: msg.role,<\/p>\n<p>&#8220;content&#8221;: msg.content.text<\/p>\n<p>})<\/p>\n<p># \u8c03\u7528 LLM<\/p>\n<p>response = self.client.chat.completions.create(<\/p>\n<p>model=&#8221;deepseek-chat&#8221;,<\/p>\n<p>messages=messages<\/p>\n<p>)<\/p>\n<p>return response.choices[0].message.content<\/p>\n<p>async def close(self):<\/p>\n<p>if self.session:<\/p>\n<p>await self.session.__aexit__(None, None, None)<\/p>\n<p>if self.transport:<\/p>\n<p>await self.transport.__aexit__(None, None, None)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u8fd9\u4e2aClient\u7684\u65b9\u5f0f\u53bb\u8c03\u7528prompt\u7684\u76f8\u5173\u80fd\u529b<\/p>\n<table>\n<tbody>\n<tr>\n<td>async def main():<\/p>\n<p>print(&#8220;&gt;&gt;&gt; \u5f00\u59cb\u521d\u59cb\u5316\u4ee3\u7801\u5ba1\u67e5\u7cfb\u7edf&#8221;)<\/p>\n<p>if len(sys.argv) &lt; 2:<\/p>\n<p>print(&#8220;\u7528\u6cd5: python client.py &lt;server.py \u8def\u5f84&gt;&#8221;)<\/p>\n<p>return<\/p>\n<p>client = CodeReviewClient()<\/p>\n<p>try:<\/p>\n<p>await client.connect(sys.argv[1])<\/p>\n<p>print(&#8220;&gt;&gt;&gt; \u7cfb\u7edf\u8fde\u63a5\u6210\u529f&#8221;)<\/p>\n<p># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>sample_code = &#8220;&#8221;&#8221;<\/p>\n<p>def calculate_fibonacci(n):<\/p>\n<p>if n &lt;= 0:<\/p>\n<p>return []<\/p>\n<p>elif n == 1:<\/p>\n<p>return [0]<\/p>\n<p>fib = [0, 1]<\/p>\n<p>for i in range(2, n):<\/p>\n<p>fib.append(fib[i-1] + fib[i-2])<\/p>\n<p>return fib<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>while True:<\/p>\n<p>print(&#8220;\\n\u8bf7\u9009\u62e9\u64cd\u4f5c\uff1a&#8221;)<\/p>\n<p>print(&#8220;1. \u4ee3\u7801\u5ba1\u67e5&#8221;)<\/p>\n<p>print(&#8220;2. \u4ee3\u7801\u89e3\u91ca&#8221;)<\/p>\n<p>print(&#8220;3. \u9000\u51fa&#8221;)<\/p>\n<p>choice = input(&#8220;&gt; &#8220;)<\/p>\n<p>if choice == &#8220;3&#8221;:<\/p>\n<p>break<\/p>\n<p>if choice == &#8220;1&#8221;:<\/p>\n<p>print(&#8220;\\n\u8bf7\u9009\u62e9\u5ba1\u67e5\u91cd\u70b9\uff1a&#8221;)<\/p>\n<p>print(&#8220;1. \u6027\u80fd&#8221;)<\/p>\n<p>print(&#8220;2. \u5b89\u5168\u6027&#8221;)<\/p>\n<p>print(&#8220;3. \u53ef\u8bfb\u6027&#8221;)<\/p>\n<p>print(&#8220;4. \u7efc\u5408&#8221;)<\/p>\n<p>focus_choice = input(&#8220;&gt; &#8220;)<\/p>\n<p>focus_map = {<\/p>\n<p>&#8220;1&#8221;: &#8220;performance&#8221;,<\/p>\n<p>&#8220;2&#8221;: &#8220;security&#8221;,<\/p>\n<p>&#8220;3&#8221;: &#8220;readability&#8221;,<\/p>\n<p>&#8220;4&#8221;: &#8220;general&#8221;<\/p>\n<p>}<\/p>\n<p>focus = focus_map.get(focus_choice, &#8220;general&#8221;)<\/p>\n<p>print(&#8220;\\n\u6b63\u5728\u5ba1\u67e5\u4ee3\u7801&#8230;&#8221;)<\/p>\n<p>response = await client.use_prompt(&#8220;code-review&#8221;, {<\/p>\n<p>&#8220;code&#8221;: sample_code,<\/p>\n<p>&#8220;language&#8221;: &#8220;Python&#8221;,<\/p>\n<p>&#8220;focus&#8221;: focus<\/p>\n<p>})<\/p>\n<p>print(&#8220;\\n\u5ba1\u67e5\u7ed3\u679c\uff1a\\n&#8221;, response)<\/p>\n<p>elif choice == &#8220;2&#8221;:<\/p>\n<p>print(&#8220;\\n\u6b63\u5728\u89e3\u91ca\u4ee3\u7801&#8230;&#8221;)<\/p>\n<p>response = await client.use_prompt(&#8220;explain-code&#8221;, {<\/p>\n<p>&#8220;code&#8221;: sample_code,<\/p>\n<p>&#8220;language&#8221;: &#8220;Python&#8221;<\/p>\n<p>})<\/p>\n<p>print(&#8220;\\n\u89e3\u91ca\uff1a\\n&#8221;, response)<\/p>\n<p>except Exception as e:<\/p>\n<p>print(f&#8221;\u53d1\u751f\u9519\u8bef: {e}&#8221;)<\/p>\n<p>finally:<\/p>\n<p>await client.close()<\/p>\n<p>print(&#8220;&gt;&gt;&gt; \u7cfb\u7edf\u5df2\u5173\u95ed&#8221;)<\/p>\n<p>if __name__ == &#8220;__main__&#8221;:<\/p>\n<p>asyncio.run(main())<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u8fd0\u884c\u8fd9\u5957\u4ee3\u7801\u4e86<\/p>\n<p>\u5206\u522b\u8fd0\u884cclient\u548cserver\u7aef\u7684py\u4ee3\u7801<\/p>\n<p>\u6700\u540e\u53bb\u8fdb\u884c\u4ea4\u4e92\uff0c\u83b7\u53d6\u5230\u63d0\u793a\u6a21\u677f\u5e76\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<p>\u6700\u540e\u603b\u7ed3\u4e0b\uff0c\u5728MCP\u4e4b\u4e2d\uff0cPrompt\u539f\u8bed\u5b9a\u4e49\u4e86\u4e00\u5957\u670d\u52a1\u5668\u7aef\u6ce8\u518c\uff0c\u5ba2\u6237\u7aef\u53d1\u73b0\u548c\u8c03\u7528\u7684\u6d88\u606f\u6a21\u677f\uff0c\u7528\u4e8e\u5411\u7740LLM\u63d0\u4f9b\u7ed3\u6784\u5316\u4e0a\u4e0b\u6587\u548c\u6307\u4ee4<\/p>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u901a\u8fc7\u7279\u5b9a\u7684\u63a5\u53e3\u6765\u68c0\u7d22\u6a21\u677f\uff0c\u5e76\u901a\u8fc7\u7279\u5b9a\u7684\u53c2\u6570\u6765\u751f\u6210\u6d88\u606f\u3002<\/p>\n<p>\u6700\u7ec8\u652f\u6301\u6a21\u677f\u7ed3\u679c\u83b7\u53d6\u3002\u4ece\u800c\u65b9\u4fbf\u4f4d\u4e8eHost\u4e0a\u7684LLM\u8c03\u7528\u5b9e\u73b0\u4e00\u7cfb\u5217\u6d88\u606f\u7684\u751f\u6210\uff0c\u4ee5Host\u4e0a\u83dc\u5355\u5b9e\u73b0\u7279\u5b9a\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MCP\u4e4b\u4e2d\u7684\u63d0\u793a\u8bcd\u5de5\u7a0b \u5728MC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5786"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5786"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5786\/revisions"}],"predecessor-version":[{"id":5788,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5786\/revisions\/5788"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5786"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}