{"id":28118,"date":"2025-11-14T08:50:33","date_gmt":"2025-11-14T00:50:33","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=28118"},"modified":"2025-11-14T08:50:33","modified_gmt":"2025-11-14T00:50:33","slug":"%e5%8f%88%e4%b8%80%e4%b8%aaai%e5%a4%a7%e6%a8%a1%e5%9e%8b%e9%a1%b9%e7%9b%ae%e5%ae%8c%e7%bb%93%ef%bc%9a%e5%9f%ba%e4%ba%8espring-ai-%e5%bf%ab%e9%80%9f%e6%90%ad%e5%bb%bamcp%e6%9c%8d%e5%8a%a1%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2025\/11\/14\/28118\/","title":{"rendered":"\u53c8\u4e00\u4e2aAI\u5927\u6a21\u578b\u9879\u76ee\u5b8c\u7ed3\uff1a\u57fa\u4e8eSpring AI \u5feb\u901f\u642d\u5efaMCP\u670d\u52a1\u52a0\u5165LLM\uff0c\u5b8c\u6574\u6e90\u7801\u5df2\u63d0\u4ea4\uff01\uff01"},"content":{"rendered":"<div class=\"article-desc\">\u5728\u6280\u672f\u9009\u578b\u4e0a\uff0cJava \u751f\u6001\u51ed\u501f\u5176\u6210\u719f\u7684\u4f01\u4e1a\u7ea7\u80fd\u529b\u548c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e3a\u6784\u5efa\u751f\u4ea7\u5c31\u7eea\u7684 MCP \u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u7406\u60f3\u7684\u57fa\u7840\u3002\u7279\u522b\u662f Spring AI \u6846\u67b6\u7684 MCP \u652f\u6301\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u57fa\u4e8e\u719f\u6089\u7684\u6280\u672f\u6808\u6784\u5efa\u53ef\u9760\u3001\u53ef\u6269\u5c55\u7684\u667a\u80fd\u670d\u52a1\u3002<\/div>\n<div id=\"postspictures\" class=\"article-content\">\n<div id=\"container\" class=\"container am-engine\" data-v-01a18e2f=\"\" data-element=\"root\">\n<p>\u5728\u4eba\u5de5\u667a\u80fd\u6280\u672f\u98de\u901f\u53d1\u5c55\u7684\u4eca\u5929\uff0c\u5927\u8bed\u8a00\u6a21\u578b\u6b63\u5728\u91cd\u5851\u6211\u4eec\u4e0e\u8f6f\u4ef6\u7cfb\u7edf\u7684\u4ea4\u4e92\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u5f3a\u5927\u7684\u6a21\u578b\u5f80\u5f80\u88ab\u56f0\u5728&#8221;\u4fe1\u606f\u5b64\u5c9b&#8221;\u4e2d\u2014\u2014\u5b83\u4eec\u62e5\u6709\u60ca\u4eba\u7684\u63a8\u7406\u80fd\u529b\uff0c\u5374\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u4f01\u4e1a\u6570\u636e\u3001\u4e1a\u52a1\u7cfb\u7edf\u6216\u5b9e\u65f6\u4fe1\u606f\u3002\u8fd9\u6b63\u662f Model Context Protocol (MCP) \u8981\u89e3\u51b3\u7684\u6838\u5fc3\u95ee\u9898\u3002<\/p>\n<p>MCP \u6b63\u5728\u6210\u4e3a\u8fde\u63a5 AI \u00a0\u6a21\u578b\u4e0e\u73b0\u5b9e\u4e16\u754c\u7684\u6807\u51c6\u5316\u6865\u6881\u3002\u8fd9\u5957\u4e13\u95e8\u4e3a\u5927\u8bed\u8a00\u6a21\u578b\u8bbe\u8ba1\u7684\u534f\u8bae\uff0c\u4e3a\u6a21\u578b\u5b89\u5168\u3001\u4e00\u81f4\u5730\u63a5\u5165\u5916\u90e8\u6570\u636e\u6e90\u548c\u5de5\u5177\u670d\u52a1\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684\u89e3\u51b3\u65b9\u6848\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u4f60\u7684 AI \u52a9\u624b\u4e0d\u4ec5\u80fd\u56de\u7b54\u95ee\u9898\uff0c\u8fd8\u80fd\u76f4\u63a5\u67e5\u8be2\u6570\u636e\u5e93\u3001\u8c03\u7528\u4e1a\u52a1\u63a5\u53e3\u3001\u751f\u6210\u5b9e\u65f6\u62a5\u544a\u2014\u2014\u8fd9\u6b63\u662f MCP \u5e26\u6765\u7684\u53d8\u9769\u3002<\/p>\n<p>\u5728\u6280\u672f\u9009\u578b\u4e0a\uff0cJava \u751f\u6001\u51ed\u501f\u5176\u6210\u719f\u7684\u4f01\u4e1a\u7ea7\u80fd\u529b\u548c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e3a\u6784\u5efa\u751f\u4ea7\u5c31\u7eea\u7684 MCP \u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u7406\u60f3\u7684\u57fa\u7840\u3002\u7279\u522b\u662f Spring AI \u6846\u67b6\u7684 MCP \u652f\u6301\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u57fa\u4e8e\u719f\u6089\u7684\u6280\u672f\u6808\u6784\u5efa\u53ef\u9760\u3001\u53ef\u6269\u5c55\u7684\u667a\u80fd\u670d\u52a1\u3002<\/p>\n<h2>\u4e00\u3001\u6df1\u5165\u7406\u89e3 MCP \u534f\u8bae<\/h2>\n<p>MCP \u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6280\u672f\u534f\u8bae\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u79cd\u5168\u65b0\u7684 AI \u5e94\u7528\u67b6\u6784\u601d\u60f3\u3002\u4e0e\u4f20\u7edf\u7684 REST API \u4e0d\u540c\uff0cMCP \u4ece\u8bbe\u8ba1\u4e4b\u521d\u5c31\u5145\u5206\u8003\u8651\u4e86\u5927\u8bed\u8a00\u6a21\u578b\u7684\u4f7f\u7528\u573a\u666f\u548c\u7279\u6027\u3002<\/p>\n<p><strong>\u534f\u8bae\u8bbe\u8ba1\u7684\u4e09\u4e2a\u6838\u5fc3\u6d1e\u5bdf\uff1a<\/strong><\/p>\n<ol class=\"list-paddingleft-1\" data-id=\"odd3d8fc-gm9Qg43m\">\n<li data-id=\"ld70c578-91a9bed2\"><strong>\u8bed\u4e49\u5316\u4ea4\u4e92<\/strong>\uff1aMCP \u4f7f\u7528\u5de5\u5177\uff08Tools\uff09\u3001\u8d44\u6e90\uff08Resources\uff09\u548c\u63d0\u793a\u6a21\u677f\uff08Prompts\uff09\u8fd9\u4e9b\u5bf9 AI \u53cb\u597d\u7684\u62bd\u8c61\uff0c\u8ba9\u6a21\u578b\u80fd\u591f\u7406\u89e3\u6bcf\u4e2a\u63a5\u53e3\u7684\u7528\u9014\u548c\u4f7f\u7528\u65b9\u5f0f\u3002<\/li>\n<li data-id=\"ld70c578-XlNZUgWc\"><strong>\u6807\u51c6\u5316\u901a\u4fe1<\/strong>\uff1a\u901a\u8fc7\u7edf\u4e00\u7684 JSON-RPC 2. 0 \u534f\u8bae\uff0cMCP \u786e\u4fdd\u4e86\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\uff0c\u907f\u514d\u4e86\u6bcf\u4e2a\u670d\u52a1\u90fd\u8981\u81ea\u5b9a\u4e49\u63a5\u53e3\u7684\u788e\u7247\u5316\u95ee\u9898\u3002<\/li>\n<li data-id=\"ld70c578-K3jiYb0E\"><strong>\u5b89\u5168\u4f18\u5148<\/strong>\uff1aMCP \u5185\u7f6e\u4e86\u8ba4\u8bc1\u548c\u6388\u6743\u673a\u5236\uff0c\u786e\u4fdd\u4f01\u4e1a\u6570\u636e\u5728 AI \u4ea4\u4e92\u8fc7\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n<p>\u4ece\u6280\u672f\u5b9e\u73b0\u89c6\u89d2\u770b\uff0cMCP \u7684\u6bcf\u4e2a&#8221;\u80fd\u529b&#8221;\u672c\u8d28\u4e0a\u90fd\u662f\u4e00\u4e2a<strong>\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u8fdc\u7a0b\u51fd\u6570<\/strong>\u3002\u5f00\u53d1\u8005\u9700\u8981\u63d0\u4f9b\u6e05\u6670\u7684 Schema\uff08\u5b9a\u4e49\u8f93\u5165\u8f93\u51fa\u7ed3\u6784\uff09\u548c\u4e30\u5bcc\u7684\u5143\u4fe1\u606f\u63cf\u8ff0\uff0c\u8ba9\u5927\u8bed\u8a00\u6a21\u578b\u80fd\u591f\u7406\u89e3\uff1a\u8fd9\u4e2a\u5de5\u5177\u662f\u505a\u4ec0\u4e48\u7684\uff1f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\uff1f\u9700\u8981\u4ec0\u4e48\u53c2\u6570\uff1f\u8fd4\u56de\u4ec0\u4e48\u7ed3\u679c\uff1f<\/p>\n<p>\u8fd9\u79cd\u8bbe\u8ba1\u54f2\u5b66\u4f7f\u5f97 MCP \u4e0d\u4ec5\u662f\u4e00\u4e2a\u6280\u672f\u6807\u51c6\uff0c\u66f4\u662f\u4e00\u79cd\u4fc3\u8fdb\u4eba\u673a\u534f\u4f5c\u7684\u4ea4\u4e92\u8303\u5f0f\u3002\u5b83\u8ba9 AI \u7cfb\u7edf\u4ece\u88ab\u52a8\u7684\u95ee\u7b54\u673a\u5668\u8f6c\u53d8\u4e3a\u80fd\u591f\u4e3b\u52a8\u64cd\u4f5c\u4e1a\u52a1\u7cfb\u7edf\u7684\u667a\u80fd\u52a9\u624b\u3002<\/p>\n<h2>\u4e8c\u3001Spring AI MCP\u6280\u672f\u4f18\u52bf<\/h2>\n<p>\u5728\u6280\u672f\u9009\u578b\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u4e86 Spring AI MCP \u800c\u975e Python \u751f\u6001\u7684 FastMCP\uff0c\u8fd9\u80cc\u540e\u6709\u7740\u6df1\u5c42\u7684\u6280\u672f\u8003\u91cf\u548c\u4f01\u4e1a\u9700\u6c42\u5206\u6790\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u9009\u62e9 Java + Spring AI \u6280\u672f\u6808\uff1f<\/strong><\/p>\n<p><strong>\uff081\uff09\u7c7b\u578b\u5b89\u5168\u7684\u575a\u5b9e\u4fdd\u969c<\/strong><\/p>\n<p>Java \u7684\u5f3a\u7c7b\u578b\u7cfb\u7edf\u5728\u6784\u5efa\u4f01\u4e1a\u7ea7\u5e94\u7528\u65f6\u63d0\u4f9b\u4e86\u65e0\u53ef\u66ff\u4ee3\u7684\u4f18\u52bf\u3002\u7f16\u8bd1\u671f\u7684\u7c7b\u578b\u68c0\u67e5\u80fd\u591f\u6355\u83b7\u5927\u90e8\u5206\u6f5c\u5728\u9519\u8bef\uff0c\u8fd9\u5728\u5904\u7406\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u6570\u636e\u8f6c\u6362\u65f6\u5c24\u4e3a\u91cd\u8981\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5728\u91d1\u878d\u6216\u533b\u7597\u7b49\u5bf9\u51c6\u786e\u6027\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\u4e2d\uff0c\u7c7b\u578b\u5b89\u5168\u4e0d\u662f\u53ef\u9009\u9879\uff0c\u800c\u662f\u5fc5\u9009\u9879\u3002<\/p>\n<p><strong>\uff082\uff09\u6210\u719f\u7684\u4f9d\u8d56\u6ce8\u5165\u4f53\u7cfb<\/strong><\/p>\n<p>Spring \u6846\u67b6\u7684 IOC \u5bb9\u5668\u8ba9\u7ec4\u4ef6\u7ba1\u7406\u548c\u4f9d\u8d56\u6ce8\u5165\u53d8\u5f97\u4f18\u96c5\u800c\u9ad8\u6548\u3002\u8fd9\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u7279\u522b\u9002\u5408 MCP \u670d\u52a1\u5668\u7684\u67b6\u6784\uff0c\u56e0\u4e3a\u5de5\u5177\u670d\u52a1\u901a\u5e38\u9700\u8981\u4f9d\u8d56\u6570\u636e\u5e93\u8fde\u63a5\u3001\u5916\u90e8 API \u5ba2\u6237\u7aef\u3001\u914d\u7f6e\u7ba1\u7406\u7b49\u591a\u4e2a\u7ec4\u4ef6\u3002<\/p>\n<p><strong>\uff083\uff09\u4f01\u4e1a\u96c6\u6210\u7684\u4e30\u5bcc\u751f\u6001<\/strong><\/p>\n<p>Spring \u751f\u6001\u63d0\u4f9b\u4e86\u4e0e\u5404\u79cd\u4f01\u4e1a\u7cfb\u7edf\u96c6\u6210\u7684\u6210\u719f\u89e3\u51b3\u65b9\u6848\u3002\u65e0\u8bba\u662f\u6570\u636e\u5e93\u8bbf\u95ee\uff08Spring Data\uff09\u3001\u6d88\u606f\u961f\u5217\uff08Spring \u00a0Integration\uff09\u3001\u5b89\u5168\u8ba4\u8bc1\uff08Spring Security\uff09\u8fd8\u662f\u76d1\u63a7\u7ba1\u7406\uff08Spring Boot \u00a0Actuator\uff09\uff0c\u90fd\u6709\u73b0\u6210\u7684\u7ec4\u4ef6\u53ef\u4ee5\u590d\u7528\u3002<\/p>\n<p><strong>\uff084\uff09\u751f\u4ea7\u73af\u5883\u7684\u5b8c\u5907\u652f\u6301<\/strong><\/p>\n<p>\u4ece\u914d\u7f6e\u7ba1\u7406\u3001\u5065\u5eb7\u68c0\u67e5\u5230\u6027\u80fd\u76d1\u63a7\u548c\u65e5\u5fd7\u8ffd\u8e2a\uff0cSpring Boot \u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u751f\u4ea7\u5c31\u7eea\u7684\u7279\u6027\u3002\u8fd9\u4e9b\u529f\u80fd\u5bf9\u4e8e\u786e\u4fdd MCP \u670d\u52a1\u5668\u5728\u771f\u5b9e\u4e1a\u52a1\u73af\u5883\u4e2d\u7684\u7a33\u5b9a\u8fd0\u884c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\uff085\uff09\u56e2\u961f\u6280\u672f\u6808\u7684\u5ef6\u7eed\u6027<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5df2\u7ecf\u62e5\u6709 Java \u6280\u672f\u79ef\u7d2f\u7684\u56e2\u961f\uff0c\u4f7f\u7528 Spring AI MCP \u53ef\u4ee5\u6700\u5927\u9650\u5ea6\u5730\u5229\u7528\u73b0\u6709\u77e5\u8bc6\u548c\u5de5\u5177\u94fe\uff0c\u964d\u4f4e\u5b66\u4e60\u6210\u672c\uff0c\u52a0\u5feb\u9879\u76ee\u4ea4\u4ed8\u901f\u5ea6\u3002<\/p>\n<h2>\u4e09\u3001\u5b9e\u6218\u6f14\u7ec3<\/h2>\n<h3>3.1 \u73af\u5883\u51c6\u5907\u4e0e\u9879\u76ee\u521d\u59cb\u5316<\/h3>\n<p>\u8ba9\u6211\u4eec\u4ece\u6700\u57fa\u7840\u7684\u73af\u5883\u642d\u5efa\u5f00\u59cb\u3002\u9996\u5148\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\uff1a<\/p>\n<ul data-id=\"u738a58b-aJW42Q2W\">\n<li data-id=\"ld70c578-lYDQCFZi\">JDK 17 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li data-id=\"ld70c578-aoVCWFfQ\">Maven 3.6+ \u6216 Gradle 7+<\/li>\n<li data-id=\"ld70c578-TJPYkJSS\">\u652f\u6301 Spring Boot 3.x \u7684 IDE<\/li>\n<\/ul>\n<p>\u521b\u5efa\u9879\u76ee\u65f6\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528 Spring Initializr \u751f\u6210\u9879\u76ee\u9aa8\u67b6\uff0c\u786e\u4fdd\u4f9d\u8d56\u7248\u672c\u7684\u4e00\u81f4\u6027\u3002\u5728\u00a0<code>pom.xml<\/code>\u00a0\u4e2d\uff0c\u9664\u4e86\u57fa\u7840\u7684 Spring Boot \u4f9d\u8d56\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0 MCP \u76f8\u5173\u7684\u7279\u5b9a\u4f9d\u8d56\u3002<\/p>\n<p><strong>\u4f9d\u8d56\u9009\u62e9\u7684\u6df1\u5c42\u8003\u91cf\uff1a<\/strong><\/p>\n<p>\u9009\u62e9\u00a0<code>spring-ai-mcp-server-spring-boot-starter<\/code>\u00a0\u800c\u4e0d\u662f\u57fa\u7840\u7684\u624b\u52a8\u914d\u7f6e\uff0c\u662f\u56e0\u4e3a starter \u63d0\u4f9b\u4e86\u81ea\u52a8\u914d\u7f6e\u3001\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u4ee5\u53ca\u4e0e Spring \u751f\u6001\u7684\u65e0\u7f1d\u96c6\u6210\u3002\u8fd9\u663e\u8457\u964d\u4f4e\u4e86\u914d\u7f6e\u590d\u6742\u5ea6\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u521b\u5efaSpringBoot\u9879\u76ee\uff0c\u6dfb\u52a0\u5982\u4e0b\u6838\u5fc3\u4f9d\u8d56\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_0\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">&lt;dependencies&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\r\n        &lt;version&gt;3.2.0&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.ai&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-ai-mcp-server-spring-boot-starter&lt;\/artifactId&gt;\r\n        &lt;version&gt;1.0.0-M6&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;<\/code><\/pre>\n<ul id=\"code_id_0\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>3.2 \u6838\u5fc3\u670d\u52a1\u67b6\u6784\u8bbe\u8ba1<\/h3>\n<p>\u5728\u5b9e\u73b0\u5177\u4f53\u529f\u80fd\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u7406\u89e3 Spring AI MCP \u670d\u52a1\u5668\u7684\u6838\u5fc3\u67b6\u6784\u7ec4\u4ef6\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_1\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ Application.java - \u670d\u52a1\u542f\u52a8\u5165\u53e3\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.ai.tool.ToolCallbackProvider;\r\nimport org.springframework.ai.tool.method.MethodToolCallbackProvider;\r\nimport org.springframework.context.annotation.Bean;\r\n\r\n@SpringBootApplication\r\npublicclass Application {\r\n\r\n    public static void main(String[] args) {\r\n        SpringApplication.run(Application.class, args);\r\n    }\r\n\r\n    @Bean\r\n    public ToolCallbackProvider mathTools(MathService mathService) {\r\n        return MethodToolCallbackProvider.builder()\r\n                .toolObjects(mathService)\r\n                .build();\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_1\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u8fd9\u4e2a\u7b80\u5355\u7684\u542f\u52a8\u7c7b\u80cc\u540e\u5305\u542b\u4e86 Spring AI MCP \u7684\u667a\u80fd\u8bbe\u8ba1\uff1a<\/p>\n<ul data-id=\"u738a58b-VV1a1ESb\">\n<li data-id=\"ld70c578-NfYbrUqg\"><strong>\u81ea\u52a8\u914d\u7f6e\u673a\u5236<\/strong>\uff1aSpring Boot \u4f1a\u81ea\u52a8\u914d\u7f6e MCP \u670d\u52a1\u5668\u7aef\u70b9\u3001\u6d88\u606f\u5904\u7406\u548c\u4f20\u8f93\u5c42<\/li>\n<li data-id=\"ld70c578-P0Sh0h8f\"><strong>\u5de5\u5177\u53d1\u73b0\u7cfb\u7edf<\/strong>\uff1a\u901a\u8fc7\u00a0<code>ToolCallbackProvider<\/code>\u00a0\u81ea\u52a8\u626b\u63cf\u548c\u6ce8\u518c\u6240\u6709\u5e26\u6709\u00a0<code>@Tool<\/code>\u00a0\u6ce8\u89e3\u7684\u65b9\u6cd5<\/li>\n<li data-id=\"ld70c578-mcXh4XYb\"><strong>\u751f\u547d\u5468\u671f\u7ba1\u7406<\/strong>\uff1aSpring \u5bb9\u5668\u8d1f\u8d23\u7ec4\u4ef6\u7684\u521b\u5efa\u3001\u4f9d\u8d56\u6ce8\u5165\u548c\u9500\u6bc1<\/li>\n<\/ul>\n<h3>3.3 \u4e1a\u52a1\u5de5\u5177\u7684\u5b9e\u73b0<\/h3>\n<p>\u5de5\u5177\uff08Tools\uff09\u662f MCP \u670d\u52a1\u5668\u7684\u6838\u5fc3\u80fd\u529b\u8f7d\u4f53\u3002\u6bcf\u4e2a\u5de5\u5177\u90fd\u5e94\u8be5\u8bbe\u8ba1\u5f97\u4e13\u6ce8\u3001\u53ef\u590d\u7528\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_2\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ MathService.java - \u6570\u5b66\u8ba1\u7b97\u5de5\u5177\r\nimport org.springframework.ai.tool.annotation.Tool;\r\nimport org.springframework.ai.tool.annotation.ToolParam;\r\nimport org.springframework.stereotype.Service;\r\n\r\n@Service\r\npublicclass MathService {\r\n\r\n    @Tool(name = \"add\", description = \"Add two integers and return the sum\")\r\n    public int add(\r\n            @ToolParam(description = \"First integer operand\") int a,\r\n            @ToolParam(description = \"Second integer operand\") int b) {\r\n        \/\/ \u8f93\u5165\u9a8c\u8bc1\u548c\u4e1a\u52a1\u903b\u8f91\u5904\u7406\r\n        if (a &lt; 0 || b &lt; 0) {\r\n            thrownew IllegalArgumentException(\"Negative numbers are not supported\");\r\n        }\r\n        return a + b;\r\n    }\r\n\r\n    @Tool(name = \"multiply\", description = \"Multiply two numbers and return the product\")\r\n    public double multiply(\r\n            @ToolParam(description = \"First number\") double a,\r\n            @ToolParam(description = \"Second number\") double b) {\r\n        \/\/ \u5904\u7406\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\r\n        BigDecimal result = BigDecimal.valueOf(a).multiply(BigDecimal.valueOf(b));\r\n        return result.doubleValue();\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_2\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>\u5de5\u5177\u8bbe\u8ba1\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong><\/p>\n<ol class=\"list-paddingleft-1\" data-id=\"odd3d8fc-kLG6hRjX\">\n<li data-id=\"ld70c578-Omn5nmaY\"><strong>\u6e05\u6670\u7684\u547d\u540d\u548c\u63cf\u8ff0<\/strong>\uff1a\u5de5\u5177\u540d\u79f0\u5e94\u8be5\u76f4\u89c2\uff0c\u63cf\u8ff0\u5e94\u8be5\u51c6\u786e\u8bf4\u660e\u529f\u80fd\u3001\u8f93\u5165\u8f93\u51fa\u548c\u53ef\u80fd\u7684\u526f\u4f5c\u7528\u3002<\/li>\n<li data-id=\"ld70c578-k6f3qfKt\"><strong>\u5b8c\u5584\u7684\u53c2\u6570\u6ce8\u89e3<\/strong>\uff1a\u6bcf\u4e2a\u53c2\u6570\u90fd\u5e94\u8be5\u6709\u8be6\u7ec6\u7684\u63cf\u8ff0\uff0c\u5e2e\u52a9 AI \u6a21\u578b\u7406\u89e3\u4f55\u65f6\u4ee5\u53ca\u5982\u4f55\u63d0\u4f9b\u8fd9\u4e2a\u53c2\u6570\u3002<\/li>\n<li data-id=\"ld70c578-v806a8lj\"><strong>\u5065\u58ee\u7684\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u5de5\u5177\u5e94\u8be5\u80fd\u591f\u5904\u7406\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\uff0c\u5e76\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li data-id=\"ld70c578-aD6VjYX8\"><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u4e8e\u53ef\u80fd\u88ab\u9891\u7e41\u8c03\u7528\u7684\u5de5\u5177\uff0c\u8981\u8003\u8651\u6027\u80fd\u4f18\u5316\u548c\u8d44\u6e90\u7ba1\u7406\u3002<\/li>\n<\/ol>\n<h3>3.4 \u8d44\u6e90\u914d\u7f6e\u4e0e\u7ba1\u7406\u7b56\u7565<\/h3>\n<p>\u8d44\u6e90\uff08Resources\uff09\u5728 MCP \u4e2d\u4ee3\u8868\u53ea\u8bfb\u6570\u636e\uff0c\u5b83\u4eec\u53ef\u4ee5\u662f\u9759\u6001\u914d\u7f6e\u4fe1\u606f\uff0c\u4e5f\u53ef\u4ee5\u662f\u57fa\u4e8e\u53c2\u6570\u7684\u52a8\u6001\u6570\u636e\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_3\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ ResourceService.java - \u8d44\u6e90\u7ba1\u7406\u670d\u52a1\r\nimport org.springframework.ai.tool.annotation.Tool;\r\nimport org.springframework.ai.tool.annotation.ToolParam;\r\nimport org.springframework.stereotype.Service;\r\nimport java.util.Map;\r\nimport java.time.LocalDateTime;\r\nimport java.time.format.DateTimeFormatter;\r\n\r\n@Service\r\npublicclass ResourceService {\r\n\r\n    @Tool(name = \"get_version\", description = \"Retrieve current server version and build information\")\r\n    public Map&lt;String, Object&gt; getVersion() {\r\n        return Map.of(\r\n            \"version\", \"1.0.0\",\r\n            \"build_time\", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),\r\n            \"environment\", \"production\"\r\n        );\r\n    }\r\n\r\n    @Tool(name = \"get_user_profile\", description = \"Retrieve detailed user profile information by user ID\")\r\n    public Map&lt;String, Object&gt; getUserProfile(\r\n            @ToolParam(description = \"Unique identifier of the user\") int userId) {\r\n        \r\n        \/\/ \u6a21\u62df\u4ece\u6570\u636e\u5e93\u6216\u5916\u90e8\u670d\u52a1\u83b7\u53d6\u7528\u6237\u4fe1\u606f\r\n        \/\/ \u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u8fd9\u91cc\u4f1a\u96c6\u6210\u771f\u5b9e\u7684\u6570\u636e\u6e90\r\n        return Map.of(\r\n            \"user_id\", userId,\r\n            \"name\", \"User \" + userId,\r\n            \"status\", \"active\",\r\n            \"created_at\", \"2024-01-01T00:00:00\",\r\n            \"last_login\", LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),\r\n            \"permissions\", new String[]{\"read\", \"write\", \"execute\"}\r\n        );\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_3\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>\u8d44\u6e90\u8bbe\u8ba1\u7684\u5173\u952e\u8003\u8651\uff1a<\/strong><\/p>\n<ol class=\"list-paddingleft-1\" data-id=\"odd3d8fc-ePC33DUj\">\n<li data-id=\"ld70c578-xd4WAtON\"><strong>\u6570\u636e\u4e00\u81f4\u6027<\/strong>\uff1a\u786e\u4fdd\u8d44\u6e90\u6570\u636e\u5728\u4e0d\u540c\u8c03\u7528\u4e4b\u95f4\u4fdd\u6301\u4e00\u81f4<\/li>\n<li data-id=\"ld70c578-uguF6rYD\"><strong>\u7f13\u5b58\u7b56\u7565<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u7ecf\u5e38\u53d8\u5316\u7684\u6570\u636e\uff0c\u8003\u8651\u5b9e\u73b0\u7f13\u5b58\u673a\u5236<\/li>\n<li data-id=\"ld70c578-FLKtAz2K\"><strong>\u6570\u636e\u8131\u654f<\/strong>\uff1a\u654f\u611f\u4fe1\u606f\u5e94\u8be5\u5728\u8fd4\u56de\u524d\u8fdb\u884c\u9002\u5f53\u7684\u8131\u654f\u5904\u7406<\/li>\n<li data-id=\"ld70c578-Yl2VoeG7\"><strong>\u7248\u672c\u7ba1\u7406<\/strong>\uff1a\u8d44\u6e90\u7ed3\u6784\u7684\u53d8\u5316\u5e94\u8be5\u8003\u8651\u7248\u672c\u517c\u5bb9\u6027<\/li>\n<\/ol>\n<h3>3.5 \u914d\u7f6e\u7ba1\u7406<\/h3>\n<p>MCP \u670d\u52a1\u5668\u7684\u914d\u7f6e\u7ba1\u7406\u9700\u8981\u5e73\u8861\u7075\u6d3b\u6027\u548c\u4e25\u8c28\u6027\u3002\u6211\u4eec\u91c7\u7528\u5206\u5c42\u914d\u7f6e\u7b56\u7565\uff0c\u9002\u5e94\u4e0d\u540c\u73af\u5883\u7684\u9700\u6c42\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_4\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\"># application.yml - \u57fa\u7840\u914d\u7f6e\r\nserver:\r\nport:8080\r\nservlet:\r\n    context-path:\/api\r\n\r\nspring:\r\napplication:\r\n    name:mcp-server\r\nai:\r\n    mcp:\r\n      server:\r\n        name:enterprise-mcp-server\r\n        version:1.0.0\r\n        type:ASYNC\r\n        sse-endpoint:\/sse\r\n        sse-message-endpoint:\/mcp\/messages\r\n        # \u9ad8\u7ea7\u914d\u7f6e\u9009\u9879\r\n        max-concurrent-requests:100\r\n        request-timeout:30s\r\n\r\nmanagement:\r\nendpoints:\r\n    web:\r\n      exposure:\r\n        include:health,info,metrics,prometheus\r\nendpoint:\r\n    health:\r\n      show-details:always\r\n    metrics:\r\n      enabled:true\r\n\r\nlogging:\r\nlevel:\r\n    org.springframework.ai:INFO\r\n    com.yourcompany.mcp:DEBUG<\/code><\/pre>\n<ul id=\"code_id_4\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>\u914d\u7f6e\u8bbe\u8ba1\u7684\u5de5\u7a0b\u8003\u91cf\uff1a<\/strong><\/p>\n<ol class=\"list-paddingleft-1\" data-id=\"odd3d8fc-SGCgRQgX\">\n<li data-id=\"ld70c578-q1T3LbYU\"><strong>\u73af\u5883\u9694\u79bb<\/strong>\uff1a\u4f7f\u7528 Spring Profile \u7ba1\u7406\u4e0d\u540c\u73af\u5883\u7684\u914d\u7f6e<\/li>\n<li data-id=\"ld70c578-hTTRP9mS\"><strong>\u5b89\u5168\u654f\u611f\u4fe1\u606f<\/strong>\uff1a\u5bc6\u7801\u3001\u5bc6\u94a5\u7b49\u654f\u611f\u4fe1\u606f\u5e94\u8be5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u6216\u914d\u7f6e\u4e2d\u5fc3\u7ba1\u7406<\/li>\n<li data-id=\"ld70c578-IKlmuOqo\"><strong>\u6027\u80fd\u8c03\u4f18\u53c2\u6570<\/strong>\uff1a\u6839\u636e\u9884\u671f\u8d1f\u8f7d\u8c03\u6574\u8fde\u63a5\u6570\u3001\u8d85\u65f6\u65f6\u95f4\u7b49\u53c2\u6570<\/li>\n<li data-id=\"ld70c578-Evx8xC3g\"><strong>\u76d1\u63a7\u548c\u53ef\u89c2\u6d4b\u6027<\/strong>\uff1a\u914d\u7f6e\u9002\u5f53\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u76d1\u63a7\u7aef\u70b9<\/li>\n<\/ol>\n<h2>\u56db\u3001\u9ad8\u7ea7\u7279\u6027\u4e0e\u5b9e\u8df5<\/h2>\n<h3>4.1 \u4e0a\u4e0b\u6587\u611f\u77e5\u7684\u667a\u80fd\u5de5\u5177<\/h3>\n<p>\u5728\u590d\u6742\u7684\u4e1a\u52a1\u573a\u666f\u4e2d\uff0c\u5de5\u5177\u5f80\u5f80\u9700\u8981\u8bbf\u95ee\u4f1a\u8bdd\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002Spring AI MCP \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4e0a\u4e0b\u6587\u652f\u6301\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_5\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ ContextAwareService.java - \u4e0a\u4e0b\u6587\u611f\u77e5\u670d\u52a1\r\nimport org.springframework.ai.tool.annotation.Tool;\r\nimport org.springframework.ai.tool.annotation.ToolParam;\r\nimport org.springframework.stereotype.Service;\r\nimport org.springframework.web.client.RestTemplate;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\n\r\n@Service\r\npublicclass ContextAwareService {\r\n    \r\n    privatefinal RestTemplate restTemplate;\r\n    privatestaticfinal Logger logger = LoggerFactory.getLogger(ContextAwareService.class);\r\n    \r\n    public ContextAwareService(RestTemplate restTemplate) {\r\n        this.restTemplate = restTemplate;\r\n    }\r\n\r\n    @Tool(name = \"summarize_content\", \r\n          description = \"Fetch content from a URL and generate a concise summary\")\r\n    public String summarizeContent(\r\n            @ToolParam(description = \"URL of the content to summarize\") String url) {\r\n        \r\n        \/\/ \u8bb0\u5f55\u8be6\u7ec6\u7684\u64cd\u4f5c\u65e5\u5fd7\r\n        logger.info(\"Starting content summarization for URL: {}\", url);\r\n        long startTime = System.currentTimeMillis();\r\n        \r\n        try {\r\n            \/\/ \u9a8c\u8bc1 URL \u683c\u5f0f\r\n            if (!isValidUrl(url)) {\r\n                thrownew IllegalArgumentException(\"Invalid URL format: \" + url);\r\n            }\r\n            \r\n            \/\/ \u83b7\u53d6\u8fdc\u7a0b\u5185\u5bb9\r\n            logger.debug(\"Fetching content from: {}\", url);\r\n            String content = fetchContentSafely(url);\r\n            \r\n            if (content == null || content.trim().isEmpty()) {\r\n                return\"No content available from the provided URL\";\r\n            }\r\n            \r\n            \/\/ \u751f\u6210\u667a\u80fd\u6458\u8981\r\n            String summary = generateIntelligentSummary(content);\r\n            long processingTime = System.currentTimeMillis() - startTime;\r\n            \r\n            logger.info(\"Successfully summarized content in {} ms\", processingTime);\r\n            return String.format(\"Summary (%d chars): %s\", summary.length(), summary);\r\n            \r\n        } catch (Exception e) {\r\n            logger.error(\"Error summarizing content from URL: {}\", url, e);\r\n            return\"Error processing content: \" + e.getMessage();\r\n        }\r\n    }\r\n    \r\n    private boolean isValidUrl(String url) {\r\n        return url != null &amp;&amp; \r\n               (url.startsWith(\"http:\/\/\") || url.startsWith(\"https:\/\/\"));\r\n    }\r\n    \r\n    private String fetchContentSafely(String url) {\r\n        try {\r\n            \/\/ \u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4\r\n            return restTemplate.getForObject(url, String.class);\r\n        } catch (Exception e) {\r\n            logger.warn(\"Failed to fetch content from URL: {}\", url, e);\r\n            returnnull;\r\n        }\r\n    }\r\n    \r\n    private String generateIntelligentSummary(String content) {\r\n        \/\/ \u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u8fd9\u91cc\u53ef\u4ee5\u96c6\u6210 AI \u6458\u8981\u670d\u52a1\r\n        \/\/ \u5f53\u524d\u5b9e\u73b0\u63d0\u4f9b\u57fa\u7840\u7684\u6587\u672c\u5904\u7406\r\n        String cleanContent = content.replaceAll(\"\\\\s+\", \" \").trim();\r\n        \r\n        \/\/ \u667a\u80fd\u622a\u65ad\uff0c\u5c3d\u91cf\u5728\u53e5\u5b50\u8fb9\u754c\u5904\u65ad\u5f00\r\n        int maxLength = 200;\r\n        if (cleanContent.length() &lt;= maxLength) {\r\n            return cleanContent;\r\n        }\r\n        \r\n        String truncated = cleanContent.substring(0, maxLength);\r\n        int lastSentenceEnd = Math.max(\r\n            truncated.lastIndexOf('.'),\r\n            Math.max(\r\n                truncated.lastIndexOf('!'),\r\n                truncated.lastIndexOf('?')\r\n            )\r\n        );\r\n        \r\n        if (lastSentenceEnd &gt; maxLength * 0.6) {\r\n            return truncated.substring(0, lastSentenceEnd + 1) + \"..\";\r\n        }\r\n        \r\n        return truncated + \"...\";\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_5\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<li>43.<\/li>\n<li>44.<\/li>\n<li>45.<\/li>\n<li>46.<\/li>\n<li>47.<\/li>\n<li>48.<\/li>\n<li>49.<\/li>\n<li>50.<\/li>\n<li>51.<\/li>\n<li>52.<\/li>\n<li>53.<\/li>\n<li>54.<\/li>\n<li>55.<\/li>\n<li>56.<\/li>\n<li>57.<\/li>\n<li>58.<\/li>\n<li>59.<\/li>\n<li>60.<\/li>\n<li>61.<\/li>\n<li>62.<\/li>\n<li>63.<\/li>\n<li>64.<\/li>\n<li>65.<\/li>\n<li>66.<\/li>\n<li>67.<\/li>\n<li>68.<\/li>\n<li>69.<\/li>\n<li>70.<\/li>\n<li>71.<\/li>\n<li>72.<\/li>\n<li>73.<\/li>\n<li>74.<\/li>\n<li>75.<\/li>\n<li>76.<\/li>\n<li>77.<\/li>\n<li>78.<\/li>\n<li>79.<\/li>\n<li>80.<\/li>\n<li>81.<\/li>\n<li>82.<\/li>\n<li>83.<\/li>\n<li>84.<\/li>\n<li>85.<\/li>\n<li>86.<\/li>\n<li>87.<\/li>\n<li>88.<\/li>\n<li>89.<\/li>\n<li>90.<\/li>\n<li>91.<\/li>\n<li>92.<\/li>\n<li>93.<\/li>\n<li>94.<\/li>\n<li>95.<\/li>\n<li>96.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>4.2 \u5b89\u5168\u67b6\u6784\u4e0e\u8ba4\u8bc1\u6388\u6743<\/h3>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5b89\u5168\u662f\u4e0d\u53ef\u59a5\u534f\u7684\u8981\u6c42\u3002\u6211\u4eec\u91c7\u7528\u591a\u5c42\u5b89\u5168\u9632\u62a4\u7b56\u7565\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_6\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ SecurityConfig.java - \u5b89\u5168\u914d\u7f6e\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.security.config.annotation.web.builders.HttpSecurity;\r\nimport org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;\r\nimport org.springframework.security.web.SecurityFilterChain;\r\nimport org.springframework.security.config.http.SessionCreationPolicy;\r\n\r\n@Configuration\r\n@EnableWebSecurity\r\npublicclass SecurityConfig {\r\n    \r\n    @Bean\r\n    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {\r\n        http\r\n            \/\/ \u7981\u7528 CSRF\uff0c\u56e0\u4e3a MCP \u4f7f\u7528\u65e0\u72b6\u6001\u8ba4\u8bc1\r\n            .csrf(csrf -&gt; csrf.disable())\r\n            \r\n            \/\/ \u914d\u7f6e\u4f1a\u8bdd\u7ba1\u7406\u4e3a\u65e0\u72b6\u6001\r\n            .sessionManagement(session -&gt; session\r\n                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)\r\n            )\r\n            \r\n            \/\/ \u914d\u7f6e\u8bf7\u6c42\u6388\u6743\r\n            .authorizeHttpRequests(authz -&gt; authz\r\n                \/\/ MCP \u7aef\u70b9\u9700\u8981\u8ba4\u8bc1\r\n                .requestMatchers(\"\/mcp\/**\").authenticated()\r\n                \/\/ \u5065\u5eb7\u68c0\u67e5\u7aef\u70b9\u5141\u8bb8\u533f\u540d\u8bbf\u95ee\r\n                .requestMatchers(\"\/actuator\/health\").permitAll()\r\n                \/\/ \u5176\u4ed6 API \u7aef\u70b9\u9700\u8981\u8ba4\u8bc1\r\n                .requestMatchers(\"\/api\/**\").authenticated()\r\n                .anyRequest().permitAll()\r\n            )\r\n            \r\n            \/\/ \u914d\u7f6e OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\r\n            .oauth2ResourceServer(oauth2 -&gt; oauth2\r\n                .jwt(jwt -&gt; {\r\n                    \/\/ JWT \u914d\u7f6e\u53ef\u4ee5\u5728 application.yml \u4e2d\u6307\u5b9a\r\n                })\r\n            );\r\n        \r\n        return http.build();\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_6\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<li>43.<\/li>\n<li>44.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>\u5b89\u5168\u8bbe\u8ba1\u7684\u6df1\u5ea6\u8003\u91cf\uff1a<\/strong><\/p>\n<ol class=\"list-paddingleft-1\" data-id=\"odd3d8fc-jqKHE9ON\">\n<li data-id=\"ld70c578-Guwa9St4\"><strong>\u9632\u5fa1\u6027\u7f16\u7a0b<\/strong>\uff1a\u6240\u6709\u8f93\u5165\u90fd\u5e94\u8be5\u9a8c\u8bc1\uff0c\u6240\u6709\u8f93\u51fa\u90fd\u5e94\u8be5\u8fc7\u6ee4<\/li>\n<li data-id=\"ld70c578-PG2RTRcr\"><strong>\u6700\u5c0f\u6743\u9650\u539f\u5219<\/strong>\uff1a\u6bcf\u4e2a\u5de5\u5177\u53ea\u5e94\u8be5\u62e5\u6709\u5b8c\u6210\u5176\u529f\u80fd\u6240\u9700\u7684\u6700\u5c0f\u6743\u9650<\/li>\n<li data-id=\"ld70c578-7vYlCrMi\"><strong>\u5ba1\u8ba1\u65e5\u5fd7<\/strong>\uff1a\u8bb0\u5f55\u6240\u6709\u7684\u654f\u611f\u64cd\u4f5c\u4ee5\u4fbf\u4e8b\u540e\u5ba1\u8ba1<\/li>\n<li data-id=\"ld70c578-b03iiPOa\"><strong>\u5bc6\u94a5\u7ba1\u7406<\/strong>\uff1a\u4f7f\u7528\u4e13\u4e1a\u7684\u5bc6\u94a5\u7ba1\u7406\u670d\u52a1\uff0c\u907f\u514d\u786c\u7f16\u7801\u5bc6\u94a5<\/li>\n<\/ol>\n<h3>4.3 \u5ba2\u6237\u7aef\u96c6\u6210\u4e0e\u6d4b\u8bd5\u7b56\u7565<\/h3>\n<p>\u4e00\u4e2a\u5b8c\u6574\u7684 MCP \u89e3\u51b3\u65b9\u6848\u9700\u8981\u5f3a\u5927\u7684\u5ba2\u6237\u7aef\u652f\u6301\u548c\u5168\u9762\u7684\u6d4b\u8bd5\u8986\u76d6\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_7\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">\/\/ MCPClientService.java - \u5ba2\u6237\u7aef\u670d\u52a1\r\nimport org.springframework.ai.chat.client.ChatClient;\r\nimport org.springframework.ai.tool.ToolCallbackProvider;\r\nimport org.springframework.boot.CommandLineRunner;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.stereotype.Service;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\n\r\n@Service\r\npublicclass MCPClientService {\r\n    \r\n    privatefinal ChatClient chatClient;\r\n    privatestaticfinal Logger logger = LoggerFactory.getLogger(MCPClientService.class);\r\n    \r\n    public MCPClientService(ChatClient chatClient) {\r\n        this.chatClient = chatClient;\r\n    }\r\n    \r\n    public String processNaturalLanguageQuery(String userQuery) {\r\n        logger.info(\"Processing natural language query: {}\", userQuery);\r\n        \r\n        try {\r\n            String result = chatClient.prompt()\r\n                    .user(userQuery)\r\n                    .call()\r\n                    .content();\r\n            \r\n            logger.debug(\"Successfully processed query, result length: {}\", \r\n                        result != null ? result.length() : 0);\r\n            return result;\r\n            \r\n        } catch (Exception e) {\r\n            logger.error(\"Failed to process query: {}\", userQuery, e);\r\n            return\"Sorry, I encountered an error while processing your request: \" + \r\n                   e.getMessage();\r\n        }\r\n    }\r\n}\r\n\r\n\/\/ TestClient.java - \u96c6\u6210\u6d4b\u8bd5\u5ba2\u6237\u7aef\r\nimport org.springframework.boot.CommandLineRunner;\r\nimport org.springframework.context.annotation.Profile;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\n@Profile(\"dev\") \/\/ \u53ea\u5728\u5f00\u53d1\u73af\u5883\u8fd0\u884c\r\npublicclass TestClient implements CommandLineRunner {\r\n    \r\n    privatefinal MCPClientService clientService;\r\n    \r\n    public TestClient(MCPClientService clientService) {\r\n        this.clientService = clientService;\r\n    }\r\n    \r\n    @Override\r\n    public void run(String... args) throws Exception {\r\n        System.out.println(\"=== MCP Server Integration Tests ===\");\r\n        System.out.println();\r\n        \r\n        \/\/ \u6d4b\u8bd5\u6570\u5b66\u8ba1\u7b97\u529f\u80fd\r\n        testMathOperations();\r\n        \r\n        \/\/ \u6d4b\u8bd5\u8d44\u6e90\u8bbf\u95ee\u529f\u80fd  \r\n        testResourceAccess();\r\n        \r\n        \/\/ \u6d4b\u8bd5\u590d\u6742\u67e5\u8be2\u5904\u7406\r\n        testComplexQueries();\r\n        \r\n        System.out.println(\"=== All Tests Completed ===\");\r\n    }\r\n    \r\n    private void testMathOperations() {\r\n        System.out.println(\"1. Testing Math Operations:\");\r\n        \r\n        String[] mathQueries = {\r\n            \"Calculate 15 + 25\",\r\n            \"What is 6.5 multiplied by 4.2?\",\r\n            \"Add 100 and 200 together\"\r\n        };\r\n        \r\n        for (String query : mathQueries) {\r\n            System.out.println(\"   Query: \" + query);\r\n            String result = clientService.processNaturalLanguageQuery(query);\r\n            System.out.println(\"   Result: \" + result);\r\n            System.out.println();\r\n        }\r\n    }\r\n    \r\n    private void testResourceAccess() {\r\n        System.out.println(\"2. Testing Resource Access:\");\r\n        \r\n        String[] resourceQueries = {\r\n            \"What version is the server running?\",\r\n            \"Get information for user ID 12345\",\r\n            \"Show me the server status\"\r\n        };\r\n        \r\n        for (String query : resourceQueries) {\r\n            System.out.println(\"   Query: \" + query);\r\n            String result = clientService.processNaturalLanguageQuery(query);\r\n            System.out.println(\"   Result: \" + result);\r\n            System.out.println();\r\n        }\r\n    }\r\n    \r\n    private void testComplexQueries() {\r\n        System.out.println(\"3. Testing Complex Queries:\");\r\n        \r\n        String[] complexQueries = {\r\n            \"First add 10 and 20, then multiply the result by 3\",\r\n            \"Get the server version and then show me user 999's profile\"\r\n        };\r\n        \r\n        for (String query : complexQueries) {\r\n            System.out.println(\"   Query: \" + query);\r\n            String result = clientService.processNaturalLanguageQuery(query);\r\n            System.out.println(\"   Result: \" + result);\r\n            System.out.println();\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_7\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<li>43.<\/li>\n<li>44.<\/li>\n<li>45.<\/li>\n<li>46.<\/li>\n<li>47.<\/li>\n<li>48.<\/li>\n<li>49.<\/li>\n<li>50.<\/li>\n<li>51.<\/li>\n<li>52.<\/li>\n<li>53.<\/li>\n<li>54.<\/li>\n<li>55.<\/li>\n<li>56.<\/li>\n<li>57.<\/li>\n<li>58.<\/li>\n<li>59.<\/li>\n<li>60.<\/li>\n<li>61.<\/li>\n<li>62.<\/li>\n<li>63.<\/li>\n<li>64.<\/li>\n<li>65.<\/li>\n<li>66.<\/li>\n<li>67.<\/li>\n<li>68.<\/li>\n<li>69.<\/li>\n<li>70.<\/li>\n<li>71.<\/li>\n<li>72.<\/li>\n<li>73.<\/li>\n<li>74.<\/li>\n<li>75.<\/li>\n<li>76.<\/li>\n<li>77.<\/li>\n<li>78.<\/li>\n<li>79.<\/li>\n<li>80.<\/li>\n<li>81.<\/li>\n<li>82.<\/li>\n<li>83.<\/li>\n<li>84.<\/li>\n<li>85.<\/li>\n<li>86.<\/li>\n<li>87.<\/li>\n<li>88.<\/li>\n<li>89.<\/li>\n<li>90.<\/li>\n<li>91.<\/li>\n<li>92.<\/li>\n<li>93.<\/li>\n<li>94.<\/li>\n<li>95.<\/li>\n<li>96.<\/li>\n<li>97.<\/li>\n<li>98.<\/li>\n<li>99.<\/li>\n<li>100.<\/li>\n<li>101.<\/li>\n<li>102.<\/li>\n<li>103.<\/li>\n<li>104.<\/li>\n<li>105.<\/li>\n<li>106.<\/li>\n<li>107.<\/li>\n<li>108.<\/li>\n<li>109.<\/li>\n<li>110.<\/li>\n<li>111.<\/li>\n<li>112.<\/li>\n<li>113.<\/li>\n<li>114.<\/li>\n<li>115.<\/li>\n<li>116.<\/li>\n<li>117.<\/li>\n<li>118.<\/li>\n<li>119.<\/li>\n<li>120.<\/li>\n<li>121.<\/li>\n<li>122.<\/li>\n<li>123.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>4.4 \u4e0e LLM \u5e94\u7528\u96c6\u6210<\/h3>\n<p>\u90e8\u7f72\u5b8c\u6210\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u5c06 MCP \u670d\u52a1\u5668\u96c6\u6210\u5230\u5927\u8bed\u8a00\u6a21\u578b\u5e94\u7528\u4e2d\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u4e0e OpenAI API \u96c6\u6210\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_8\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\">import org.springframework.ai.chat.client.ChatClient;\r\nimport org.springframework.ai.tool.ToolCallbackProvider;\r\nimport org.springframework.web.client.RestTemplate;\r\nimport org.springframework.stereotype.Service;\r\n\r\n@Service\r\npublicclass LLMIntegrationService {\r\n    \r\n    privatefinal ChatClient chatClient;\r\n    privatefinal RestTemplate restTemplate;\r\n    \r\n    public LLMIntegrationService(ChatClient chatClient, RestTemplate restTemplate) {\r\n        this.chatClient = chatClient;\r\n        this.restTemplate = restTemplate;\r\n    }\r\n    \r\n    public String processWithLLM(String userQuery) {\r\n        \/\/ \u4f7f\u7528 MCP \u5de5\u5177\u5904\u7406\u7528\u6237\u67e5\u8be2\r\n        String toolResult = chatClient.prompt()\r\n                .user(userQuery)\r\n                .call()\r\n                .content();\r\n        \r\n        \/\/ \u5c06\u7ed3\u679c\u4f20\u9012\u7ed9 LLM \u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\r\n        String llmPrompt = String.format(\r\n            \"Based on the calculation result '%s', provide a user-friendly explanation of what this means in practical terms.\",\r\n            toolResult\r\n        );\r\n        \r\n        \/\/ \u8c03\u7528\u5916\u90e8 LLM API\uff08\u793a\u4f8b\u4f7f\u7528\u6a21\u62df\u54cd\u5e94\uff09\r\n        return enhanceWithLLM(llmPrompt);\r\n    }\r\n    \r\n    private String enhanceWithLLM(String prompt) {\r\n        \/\/ \u5b9e\u9645\u9879\u76ee\u4e2d\u8fd9\u91cc\u4f1a\u8c03\u7528\u771f\u5b9e\u7684 LLM API\r\n        \/\/ \u793a\u4f8b\u4e2d\u4f7f\u7528\u6a21\u62df\u903b\u8f91\r\n        if (prompt.contains(\"calculation\") || prompt.contains(\"result\")) {\r\n            return\"The calculation has been completed successfully. This result can be used for further analysis or decision making.\";\r\n        }\r\n        return\"I've processed your request using the available tools. Is there anything else you'd like to know?\";\r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_8\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2>\u4e94\u3001\u90e8\u7f72\u4e0e\u8fd0\u7ef4<\/h2>\n<h3>5.1 \u5bb9\u5668\u5316\u4e0e\u4e91\u539f\u751f\u90e8\u7f72<\/h3>\n<p>\u73b0\u4ee3\u5e94\u7528\u90e8\u7f72\u8d8a\u6765\u8d8a\u503e\u5411\u4e8e\u5bb9\u5668\u5316\u548c\u4e91\u539f\u751f\u67b6\u6784\u3002\u4ee5\u4e0b\u662f\u6211\u4eec\u7684 Docker \u548c Kubernetes \u914d\u7f6e\u5b9e\u8df5\u3002<\/p>\n<p><strong>Dockerfile \u4f18\u5316\uff1a<\/strong><\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_9\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\"># \u4f7f\u7528\u591a\u9636\u6bb5\u6784\u5efa\u51cf\u5c0f\u955c\u50cf\u5927\u5c0f\r\nFROM eclipse-temurin:17-jdk as builder\r\nWORKDIR \/app\r\nCOPY . .\r\nRUN .\/mvnw clean package -DskipTests\r\n\r\n# \u751f\u4ea7\u9636\u6bb5\r\nFROM eclipse-temurin:17-jre\r\nWORKDIR \/app\r\n\r\n# \u521b\u5efa\u975eroot\u7528\u6237\u8fd0\u884c\u5e94\u7528\r\nRUN groupadd -r spring &amp;&amp; useradd -r -g spring spring\r\nUSER spring\r\n\r\n# \u590d\u5236\u6784\u5efa\u4ea7\u7269\r\nCOPY --from=builder \/app\/target\/*.jar app.jar\r\n\r\n# \u914d\u7f6eJVM\u53c2\u6570\r\nENV JAVA_OPTS=\"-Xmx512m -Xms256m -XX:+UseG1GC -XX:MaxGCPauseMillis=100\"\r\n\r\nEXPOSE8080\r\n\r\n# \u4f7f\u7528 exec form \u542f\u52a8\u5e94\u7528\r\nENTRYPOINT [\"sh\", \"-c\", \"java $JAVA_OPTS -jar app.jar\"]<\/code><\/pre>\n<ul id=\"code_id_9\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>Kubernetes \u90e8\u7f72\u914d\u7f6e\uff1a<\/strong><\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_10\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\"># deployment.yaml\r\napiVersion:apps\/v1\r\nkind:Deployment\r\nmetadata:\r\nname:mcp-server\r\nlabels:\r\n    app:mcp-server\r\nspec:\r\nreplicas:3\r\nselector:\r\n    matchLabels:\r\n      app:mcp-server\r\ntemplate:\r\n    metadata:\r\n      labels:\r\n        app:mcp-server\r\n      annotations:\r\n        prometheus.io\/scrape:\"true\"\r\n        prometheus.io\/port:\"8080\"\r\n        prometheus.io\/path:\"\/actuator\/prometheus\"\r\n    spec:\r\n      containers:\r\n      -name:mcp-server\r\n        image:your-registry\/mcp-server:latest\r\n        ports:\r\n        -containerPort:8080\r\n        env:\r\n        -name:SPRING_PROFILES_ACTIVE\r\n          value:\"prod\"\r\n        -name:JAVA_OPTS\r\n          value:\"-Xmx512m -Xms256m\"\r\n        resources:\r\n          requests:\r\n            memory:\"512Mi\"\r\n            cpu:\"250m\"\r\n          limits:\r\n            memory:\"1Gi\"\r\n            cpu:\"500m\"\r\n        livenessProbe:\r\n          httpGet:\r\n            path:\/actuator\/health\r\n            port:8080\r\n          initialDelaySeconds:60\r\n          periodSeconds:10\r\n        readinessProbe:\r\n          httpGet:\r\n            path:\/actuator\/health\/readiness\r\n            port:8080\r\n          initialDelaySeconds:30\r\n          periodSeconds:5\r\n---\r\napiVersion:v1\r\nkind:Service\r\nmetadata:\r\nname:mcp-service\r\nspec:\r\nselector:\r\n    app:mcp-server\r\nports:\r\n-port:80\r\n    targetPort:8080\r\ntype:ClusterIP<\/code><\/pre>\n<ul id=\"code_id_10\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<li>43.<\/li>\n<li>44.<\/li>\n<li>45.<\/li>\n<li>46.<\/li>\n<li>47.<\/li>\n<li>48.<\/li>\n<li>49.<\/li>\n<li>50.<\/li>\n<li>51.<\/li>\n<li>52.<\/li>\n<li>53.<\/li>\n<li>54.<\/li>\n<li>55.<\/li>\n<li>56.<\/li>\n<li>57.<\/li>\n<li>58.<\/li>\n<li>59.<\/li>\n<li>60.<\/li>\n<li>61.<\/li>\n<li>62.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>5.2 \u76d1\u63a7\u4e0e\u53ef\u89c2\u6d4b\u6027<\/h3>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5b8c\u5584\u7684\u76d1\u63a7\u4f53\u7cfb\u662f\u4fdd\u8bc1\u670d\u52a1\u53ef\u9760\u6027\u7684\u5173\u952e\u3002<\/p>\n<p><strong>\u5e94\u7528\u76d1\u63a7\u914d\u7f6e\uff1a<\/strong><\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_11\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-plain\" tabindex=\"0\"><code class=\"language-plain\"># application-prod.yml - \u751f\u4ea7\u73af\u5883\u76d1\u63a7\u914d\u7f6e\r\nmanagement:\r\nendpoints:\r\n    web:\r\n      exposure:\r\n        include:health,info,metrics,prometheus,loggers\r\nendpoint:\r\n    health:\r\n      show-details:always\r\n      show-components:always\r\n    metrics:\r\n      enabled:true\r\n    prometheus:\r\n      enabled:true\r\nmetrics:\r\n    export:\r\n      prometheus:\r\n        enabled:true\r\n    distribution:\r\n      percentiles-histogram:\r\n        \"[http.server.requests]\":true\r\n    tags:\r\n      application:${spring.application.name}\r\n      environment:prod\r\n\r\nlogging:\r\nlevel:\r\n    org.springframework.ai:INFO\r\n    com.yourcompany.mcp:INFO\r\npattern:\r\n    level:\"%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]\"\r\n\r\n# \u81ea\u5b9a\u4e49\u5065\u5eb7\u68c0\u67e5\u6307\u6807\r\nendpoints:\r\nhealth:\r\n    custom:\r\n      mcp-connection:\r\n        enabled:true\r\n      external-api:\r\n        enabled:true<\/code><\/pre>\n<ul id=\"code_id_11\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7 Spring AI MCP\uff0cJava \u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u6784\u5efa\u4f01\u4e1a\u7ea7\u7684 MCP \u670d\u52a1\u5668\uff0c\u4e3a\u5927\u578b\u8bed\u8a00\u6a21\u578b\u63d0\u4f9b\u7a33\u5b9a\u53ef\u9760\u7684\u5916\u90e8\u80fd\u529b\u63a5\u5165\u3002\u76f8\u6bd4 Python \u65b9\u6848\uff0cJava \u7248\u672c\u5728\u7c7b\u578b\u5b89\u5168\u3001\u4f01\u4e1a\u96c6\u6210\u548c\u751f\u4ea7\u5c31\u7eea\u65b9\u9762\u5177\u6709\u663e\u8457\u4f18\u52bf\u3002<\/p>\n<p>\u65e0\u8bba\u662f\u6784\u5efa\u5185\u90e8 AI \u52a9\u624b\u8fd8\u662f\u5f00\u53d1\u9762\u5411\u5ba2\u6237\u7684\u667a\u80fd\u5e94\u7528\uff0cSpring AI MCP \u90fd\u63d0\u4f9b\u4e86\u4ece\u6982\u5ff5\u9a8c\u8bc1\u5230\u751f\u4ea7\u90e8\u7f72\u7684\u5b8c\u6574\u6280\u672f\u8def\u5f84\u3002<\/p>\n<p>\u6587\u7ae0\u6765\u81ea\uff1a51CTO<\/p>\n<\/div>\n<\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_28118\" class=\"pvc_stats total_only  \" data-element-id=\"28118\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6280\u672f\u9009\u578b\u4e0a\uff0cJava \u751f\u6001\u51ed\u501f\u5176\u6210\u719f\u7684\u4f01\u4e1a\u7ea7\u80fd\u529b\u548c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e3a\u6784\u5efa\u751f\u4ea7\u5c31\u7eea\u7684 MCP \u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u7406\u60f3 [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_28118\" class=\"pvc_stats total_only  \" data-element-id=\"28118\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,23,20,80],"tags":[],"class_list":["post-28118","post","type-post","status-publish","format-standard","hentry","category-ai","category-23","category-20","category-80"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/28118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=28118"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/28118\/revisions"}],"predecessor-version":[{"id":28119,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/28118\/revisions\/28119"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=28118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=28118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=28118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}