{"id":82787,"date":"2026-07-25T12:49:19","date_gmt":"2026-07-25T04:49:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/82787.html"},"modified":"2026-07-25T12:49:19","modified_gmt":"2026-07-25T04:49:19","slug":"openapi-to-mcp-bridge-%e4%b8%80%e9%94%ae%e5%b0%86-spring-boot-openapi-%e8%bd%ac%e6%8d%a2%e4%b8%ba-mcp-%e5%b7%a5%e5%85%b7%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/82787.html","title":{"rendered":"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668"},"content":{"rendered":"<h4>&#x1f4dd; \u9879\u76ee\u4ecb\u7ecd (Project Description)<\/h4>\n<h5 style=\"background-color:transparent\">\u7b80\u4ecb<\/h5>\n<p>OpenAPI-to-MCP Bridge\u00a0\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 Python Web \u670d\u52a1\u5668&#xff0c;\u65e8\u5728\u6253\u7834\u4f20\u7edf\u540e\u7aef\u670d\u52a1\u4e0e\u5927\u8bed\u8a00\u6a21\u578b&#xff08;LLM&#xff09;Agent \u4e4b\u95f4\u7684\u58c1\u5792\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u62c9\u53d6\u73b0\u6709\u7684 Spring Boot \u5e94\u7528\u751f\u6210\u7684 OpenAPI (Swagger) \u89c4\u8303&#xff0c;\u5b9e\u65f6\u89e3\u6790\u63a5\u53e3\u5b9a\u4e49\u3001\u53c2\u6570\u7ea6\u675f\u53ca\u6570\u636e\u7ed3\u6784&#xff0c;\u5e76\u5c06\u5176\u52a8\u6001\u6ce8\u518c\u4e3a\u7b26\u5408\u00a0Model Context Protocol (MCP)\u00a0\u6807\u51c6\u7684\u5de5\u5177&#xff08;Tools&#xff09;\u3002<\/p>\n<p>\u901a\u8fc7\u672c\u9879\u76ee&#xff0c;\u4f60\u7684 LLM Agent \u65e0\u9700\u4efb\u4f55\u624b\u52a8\u914d\u7f6e&#xff0c;\u5373\u53ef\u201c\u7406\u89e3\u201d\u5e76\u8c03\u7528\u73b0\u6709\u7684 RESTful API&#xff0c;\u5b9e\u73b0\u4ece\u81ea\u7136\u8bed\u8a00\u6307\u4ee4\u5230\u540e\u7aef\u4e1a\u52a1\u903b\u8f91\u7684\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<h5>\u2728 \u6838\u5fc3\u7279\u6027<\/h5>\n<ul>\n<li>&#x1f504; \u96f6\u4ee3\u7801\u9002\u914d (Zero-Code Integration) \u53ea\u9700\u63d0\u4f9b Spring Boot \u5e94\u7528\u7684\u00a0base-url&#xff0c;\u811a\u672c\u81ea\u52a8\u83b7\u53d6\u00a0\/v3\/api-docs&#xff0c;\u65e0\u9700\u7f16\u5199\u4efb\u4f55\u80f6\u6c34\u4ee3\u7801\u5373\u53ef\u66b4\u9732\u6240\u6709 GET\/POST\/PUT\/DELETE \u63a5\u53e3\u3002<\/li>\n<li>&#x1f9e0; \u667a\u80fd Schema \u89e3\u6790 (Smart Schema Resolution) \u5185\u7f6e\u00a0prance\u00a0\u89e3\u6790\u5668&#xff0c;\u81ea\u52a8\u5904\u7406 OpenAPI \u4e2d\u7684\u00a0$ref\u00a0\u5f15\u7528\u5faa\u73af\u548c\u5d4c\u5957\u7ed3\u6784&#xff0c;\u786e\u4fdd\u590d\u6742\u5bf9\u8c61\u4e5f\u80fd\u88ab\u51c6\u786e\u8bc6\u522b\u3002<\/li>\n<li>&#x1f4dd; \u81ea\u52a8\u751f\u6210\u9ad8\u8d28\u91cf\u793a\u4f8b (Auto-Generated Examples) \u72ec\u521b\u7684\u00a0schema_to_example\u00a0\u7b97\u6cd5&#xff0c;\u6839\u636e\u5b57\u6bb5\u7c7b\u578b\u3001\u683c\u5f0f&#xff08;\u5982 date-time, uuid&#xff09;\u3001\u679a\u4e3e\u503c\u53ca\u9ed8\u8ba4\u503c&#xff0c;\u52a8\u6001\u751f\u6210\u903c\u771f\u7684 JSON \u8bf7\u6c42\u4f53\u793a\u4f8b\u3002\u8fd9\u4e0d\u4ec5\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3\u63a5\u53e3&#xff0c;\u66f4\u663e\u8457\u63d0\u5347\u4e86 LLM \u6784\u9020\u6b63\u786e\u53c2\u6570\u7684\u51c6\u786e\u7387\u3002<\/li>\n<li>&#x1f6e0;\ufe0f \u52a8\u6001\u5de5\u5177\u6784\u5efa (Dynamic Tool Generation) \u5229\u7528 Python \u5143\u7f16\u7a0b\u6280\u672f&#xff0c;\u8fd0\u884c\u65f6\u52a8\u6001\u6784\u5efa\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef\u51fd\u6570\u3002\u81ea\u52a8\u533a\u5206 Path \u53c2\u6570\u3001Query \u53c2\u6570\u548c Request Body&#xff0c;\u5e76\u751f\u6210\u5305\u542b\u8be6\u7ec6\u6587\u6863&#xff08;Docstring&#xff09;\u7684\u5de5\u5177\u63cf\u8ff0\u3002<\/li>\n<li>\u26a1 \u57fa\u4e8e FastMCP \u6807\u51c6 \u5b8c\u5168\u517c\u5bb9 MCP \u534f\u8bae&#xff0c;\u652f\u6301 SSE (Server-Sent Events) \u4f20\u8f93\u6a21\u5f0f&#xff0c;\u53ef\u65e0\u7f1d\u5bf9\u63a5 Cursor\u3001Claude Desktop \u6216\u5176\u4ed6\u652f\u6301 MCP \u7684 AI \u5ba2\u6237\u7aef\u3002<\/li>\n<\/ul>\n<h5>&#x1f31f; \u5de5\u4f5c\u539f\u7406<\/h5>\n<li>\u53d1\u73b0 (Discovery): \u542f\u52a8\u65f6\u8fde\u63a5\u6307\u5b9a\u7684 Spring Boot \u5e94\u7528&#xff0c;\u62c9\u53d6\u6700\u65b0\u7684 OpenAPI JSON \u89c4\u8303\u3002<\/li>\n<li>\u89e3\u6790 (Resolution): \u4f7f\u7528\u00a0prance\u00a0\u5c55\u5f00\u6240\u6709\u5f15\u7528&#xff0c;\u8fd8\u539f\u5b8c\u6574\u7684\u63a5\u53e3\u5b9a\u4e49\u6811\u3002<\/li>\n<li>\u589e\u5f3a (Enhancement): \u904d\u5386\u6bcf\u4e2a\u63a5\u53e3&#xff0c;\u5206\u6790\u5165\u53c2 Schema&#xff0c;\u667a\u80fd\u751f\u6210\u00a0example\u00a0\u6570\u636e&#xff0c;\u5e76\u6784\u5efa\u5305\u542b\u53c2\u6570\u8bf4\u660e\u548c\u8fd4\u56de\u9884\u671f\u7684\u81ea\u7136\u8bed\u8a00\u63cf\u8ff0\u3002<\/li>\n<li>\u6ce8\u518c (Registration): \u52a8\u6001\u521b\u5efa Python\u00a0async\u00a0\u51fd\u6570&#xff0c;\u5c01\u88c5\u00a0httpx\u00a0\u8bf7\u6c42\u903b\u8f91&#xff0c;\u5e76\u901a\u8fc7\u00a0FastMCP\u00a0\u6ce8\u518c\u4e3a Tool\u3002<\/li>\n<li>\u670d\u52a1 (Serving): \u542f\u52a8 MCP Server&#xff0c;\u7b49\u5f85 AI \u5ba2\u6237\u7aef\u8fde\u63a5\u5e76\u8c03\u7528\u5de5\u5177\u3002<\/li>\n<h5>&#x1f6e0;\ufe0f \u5feb\u901f\u5f00\u59cb<\/h5>\n<p>\u786e\u4fdd\u5df2\u5b89\u88c5\u4f9d\u8d56&#xff1a;<\/p>\n<p>pip install fastmcp httpx prance openapi-spec-validator <\/p>\n<p>\u8fd0\u884c\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>python main.py &#8211;base-url http:\/\/localhost:8080 <\/p>\n<p>\u7a0b\u5e8f\u5c06\u81ea\u52a8\u52a0\u8f7d\u00a0http:\/\/localhost:8080\/v3\/api-docs\u00a0\u5e76\u5728\u00a00.0.0.0:8000\u00a0\u542f\u52a8 MCP \u670d\u52a1\u3002<\/p>\n<h5>&#x1f4a1; \u5e94\u7528\u573a\u666f<\/h5>\n<ul>\n<li>\u9057\u7559\u7cfb\u7edf\u73b0\u4ee3\u5316: \u5feb\u901f\u8ba9\u65e7\u6709\u7684 Spring Boot \u5fae\u670d\u52a1\u5177\u5907 AI Agent \u4ea4\u4e92\u80fd\u529b\u3002<\/li>\n<li>\u5185\u90e8\u52a9\u624b\u5f00\u53d1: \u4e3a\u4f01\u4e1a\u5185\u90e8\u77e5\u8bc6\u5e93\u6216\u8fd0\u7ef4\u52a9\u624b\u81ea\u52a8\u6302\u8f7d\u6570\u636e\u5e93\u7ba1\u7406\u3001\u8ba2\u5355\u67e5\u8be2\u7b49 API \u5de5\u5177\u3002<\/li>\n<li>\u539f\u578b\u9a8c\u8bc1: \u5728 API \u8bbe\u8ba1\u9636\u6bb5&#xff0c;\u7acb\u5373\u6d4b\u8bd5 LLM \u5bf9\u63a5\u53e3\u7406\u89e3\u7684\u51c6\u786e\u6027\u3002<\/li>\n<\/ul>\n<h5>\u6280\u672f\u6808<\/h5>\n<ul>\n<li>Core: Python 3.12&#043;<\/li>\n<li>MCP Framework:\u00a0FastMCP<\/li>\n<li>HTTP Client:\u00a0httpx\u00a0(Async)<\/li>\n<li>Spec Parser:\u00a0prance\u00a0&#043;\u00a0openapi-spec-validator<\/li>\n<li>Target: Spring Boot (OpenAPI 3.0)<\/li>\n<\/ul>\n<h4 style=\"background-color:transparent\">\u4ee3\u7801<\/h4>\n<p># \u5bfc\u5165\u6240\u9700\u7684\u5e93<br \/>\nimport json<br \/>\nimport httpx<br \/>\nfrom fastmcp import FastMCP<br \/>\nimport prance  # \u7528\u4e8e\u89e3\u6790\u548c dereference OpenAPI spec<\/p>\n<p># &#8212; \u4f7f\u7528 prance \u81ea\u52a8\u89e3\u6790 OpenAPI spec &#8212;<br \/>\ndef load_and_resolve_openapi_spec(source: str) -&gt; dict:<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u4ece URL \u6216\u672c\u5730\u6587\u4ef6\u52a0\u8f7d OpenAPI spec&#xff0c;\u5e76\u81ea\u52a8\u89e3\u6790\u6240\u6709 $ref \u5f15\u7528&#xff08;dereference&#xff09;\u3002<\/p>\n<p>    \u8fd4\u56de\u4e00\u4e2a\u5b8c\u5168\u5c55\u5f00\u7684\u3001\u65e0 $ref \u7684\u89c4\u8303\u5b57\u5178\u3002<br \/>\n    &#034;&#034;&#034;<br \/>\n    parser &#061; prance.ResolvingParser(<br \/>\n        source,<br \/>\n        strict&#061;False,  # \u5bbd\u677e\u6a21\u5f0f&#xff0c;\u5bb9\u5fcd\u90e8\u5206\u975e\u6807\u51c6\u5b57\u6bb5<br \/>\n        backend&#061;&#034;openapi-spec-validator&#034;<br \/>\n    )<br \/>\n    return parser.specification<\/p>\n<p># &#8212; \u7b80\u5316\u540e\u7684 schema_to_example&#xff08;\u4e0d\u518d\u9700\u8981 resolve \u903b\u8f91&#xff09;&#8212;<br \/>\ndef schema_to_example(schema: dict, depth: int &#061; 0, max_depth: int &#061; 4):<br \/>\n    if depth &gt; max_depth or not isinstance(schema, dict):<br \/>\n        return None<\/p>\n<p>    # OpenAPI example\/enum\/default \u4f18\u5148<br \/>\n    if &#034;example&#034; in schema:<br \/>\n        return schema[&#034;example&#034;]<br \/>\n    if &#034;default&#034; in schema:<br \/>\n        return schema[&#034;default&#034;]<br \/>\n    if &#034;enum&#034; in schema and schema[&#034;enum&#034;]:<br \/>\n        return schema[&#034;enum&#034;][0]<\/p>\n<p>    # \u5904\u7406 oneOf \/ anyOf&#xff1a;\u53d6\u7b2c\u4e00\u4e2a&#xff08;\u4e3a\u4e86\u793a\u4f8b\u7a33\u5b9a&#xff09;<br \/>\n    for key in (&#034;oneOf&#034;, &#034;anyOf&#034;):<br \/>\n        if key in schema and schema[key]:<br \/>\n            return schema_to_example(schema[key][0], depth, max_depth)<\/p>\n<p>    t &#061; schema.get(&#034;type&#034;)<br \/>\n    fmt &#061; schema.get(&#034;format&#034;)<\/p>\n<p>    if t &#061;&#061; &#034;string&#034; or (t is None and &#034;properties&#034; not in schema and &#034;items&#034; not in schema):<br \/>\n        if fmt in (&#034;date-time&#034;, &#034;datetime&#034;):<br \/>\n            return &#034;2026-02-06T12:00:00Z&#034;<br \/>\n        if fmt &#061;&#061; &#034;date&#034;:<br \/>\n            return &#034;2026-02-06&#034;<br \/>\n        if fmt &#061;&#061; &#034;uuid&#034;:<br \/>\n            return &#034;00000000-0000-0000-0000-000000000000&#034;<br \/>\n        return &#034;string&#034;<\/p>\n<p>    if t &#061;&#061; &#034;integer&#034;:<br \/>\n        return 0<br \/>\n    if t &#061;&#061; &#034;number&#034;:<br \/>\n        return 0.0<br \/>\n    if t &#061;&#061; &#034;boolean&#034;:<br \/>\n        return True<\/p>\n<p>    if t &#061;&#061; &#034;array&#034;:<br \/>\n        item_schema &#061; schema.get(&#034;items&#034;, {})<br \/>\n        return [schema_to_example(item_schema, depth &#043; 1, max_depth)]<\/p>\n<p>    # object \u7c7b\u578b<br \/>\n    if t &#061;&#061; &#034;object&#034; or &#034;properties&#034; in schema or &#034;additionalProperties&#034; in schema:<br \/>\n        obj &#061; {}<br \/>\n        props &#061; schema.get(&#034;properties&#034;) or {}<br \/>\n        required &#061; set(schema.get(&#034;required&#034;, []))<\/p>\n<p>        # \u5148\u586b required \u5b57\u6bb5<br \/>\n        for name in required:<br \/>\n            if name in props:<br \/>\n                obj[name] &#061; schema_to_example(props[name], depth &#043; 1, max_depth)<\/p>\n<p>        # \u518d\u8865 1~2 \u4e2a optional \u5b57\u6bb5&#xff08;\u907f\u514d\u793a\u4f8b\u592a\u7b80\u5355&#xff09;<br \/>\n        optional_count &#061; 0<br \/>\n        for name, ps in props.items():<br \/>\n            if name in obj:<br \/>\n                continue<br \/>\n            obj[name] &#061; schema_to_example(ps, depth &#043; 1, max_depth)<br \/>\n            optional_count &#043;&#061; 1<br \/>\n            if optional_count &gt;&#061; 2:<br \/>\n                break<\/p>\n<p>        # additionalProperties \u652f\u6301<br \/>\n        addl &#061; schema.get(&#034;additionalProperties&#034;)<br \/>\n        if isinstance(addl, dict):<br \/>\n            obj.setdefault(&#034;exampleKey&#034;, schema_to_example(addl, depth &#043; 1, max_depth))<\/p>\n<p>        return obj<\/p>\n<p>    return None<\/p>\n<p>def build_request_body_example(spec: dict, details: dict):<br \/>\n    rb &#061; details.get(&#034;requestBody&#034;) or {}<br \/>\n    content &#061; rb.get(&#034;content&#034;) or {}<br \/>\n    app_json &#061; content.get(&#034;application\/json&#034;) or {}<br \/>\n    schema &#061; app_json.get(&#034;schema&#034;)<br \/>\n    if not schema:<br \/>\n        return None<br \/>\n    return schema_to_example(schema)<\/p>\n<p># \u521b\u5efa FastMCP \u5b9e\u4f8b<br \/>\nmcp &#061; FastMCP(name&#061;&#034;OpenAPI Bridge&#034;, instructions&#061;&#034;Expose OpenAPI endpoints as MCP tools&#034;)<\/p>\n<p>def register_openapi_tools(spec: dict, base_url: str):<br \/>\n    paths &#061; spec.get(&#034;paths&#034;, {})<\/p>\n<p>    for path, methods in paths.items():<br \/>\n        for method, details in methods.items():<br \/>\n            if method not in [&#034;get&#034;, &#034;post&#034;, &#034;put&#034;, &#034;delete&#034;]:<br \/>\n                continue<\/p>\n<p>            operation_id &#061; details.get(&#034;operationId&#034;, f&#034;{method}_{path.replace(&#039;\/&#039;, &#039;_&#039;).strip(&#039;_&#039;)}&#034;)<br \/>\n            description &#061; details.get(&#034;summary&#034;, details.get(&#034;description&#034;, &#034;&#034;))<\/p>\n<p>            # \u63d0\u53d6 path\/query \u53c2\u6570<br \/>\n            parameters &#061; details.get(&#034;parameters&#034;, [])<br \/>\n            param_defs &#061; []<br \/>\n            for p in parameters:<br \/>\n                if p.get(&#034;in&#034;) not in (&#034;path&#034;, &#034;query&#034;):<br \/>\n                    continue<br \/>\n                name &#061; p.get(&#034;name&#034;)<br \/>\n                if not name:<br \/>\n                    continue<br \/>\n                required &#061; bool(p.get(&#034;required&#034;, False)) or p.get(&#034;in&#034;) &#061;&#061; &#034;path&#034;<br \/>\n                param_defs.append((name, required))<\/p>\n<p>            # \u53bb\u91cd&#xff08;\u4fdd\u7559\u9996\u6b21\u51fa\u73b0&#xff09;<br \/>\n            seen &#061; set()<br \/>\n            unique_param_defs &#061; []<br \/>\n            for n, r in param_defs:<br \/>\n                if n not in seen:<br \/>\n                    seen.add(n)<br \/>\n                    unique_param_defs.append((n, r))<br \/>\n            param_defs &#061; unique_param_defs<\/p>\n<p>            # requestBody \u793a\u4f8b<br \/>\n            body_example &#061; build_request_body_example(spec, details)<\/p>\n<p>            # \u6784\u5efa docstring<br \/>\n            doc_parts &#061; [f&#034;{description}\\\\n&#034;]<br \/>\n            if body_example is not None:<br \/>\n                doc_parts.append(&#034;\\\\n    \u793a\u4f8b\u5165\u53c2(JSON body):&#034;)<br \/>\n                doc_parts.append(<br \/>\n                    &#034;        &#034; &#043; json.dumps(body_example, ensure_ascii&#061;False, indent&#061;2).replace(&#034;\\\\n&#034;, &#034;\\\\n        &#034;))<\/p>\n<p>            responses &#061; details.get(&#034;responses&#034;, {})<br \/>\n            success_resp &#061; responses.get(&#034;200&#034;) or responses.get(&#034;201&#034;) or {}<br \/>\n            if success_resp:<br \/>\n                desc &#061; success_resp.get(&#034;description&#034;, &#034;API \u8c03\u7528\u7684\u54cd\u5e94\u6587\u672c\u3002&#034;)<br \/>\n                doc_parts.append(f&#034;\\\\n    Returns:\\\\n        str: {desc}&#034;)<\/p>\n<p>            dynamic_docstring &#061; &#034;&#034;.join(doc_parts)<\/p>\n<p>            # \u52a8\u6001\u751f\u6210\u5de5\u5177\u51fd\u6570<br \/>\n            def make_tool(p&#061;path, m&#061;method, url&#061;base_url, _param_defs&#061;None, _has_body&#061;False):<br \/>\n                _param_defs &#061; _param_defs or []<\/p>\n<p>                lines &#061; [&#034;async def tool_func(&#034;]<br \/>\n                for name, required in _param_defs:<br \/>\n                    if required:<br \/>\n                        lines.append(f&#034;    {name},&#034;)<br \/>\n                    else:<br \/>\n                        lines.append(f&#034;    {name}&#061;None,&#034;)<br \/>\n                if _has_body:<br \/>\n                    lines.append(&#034;    body&#061;None,\\\\n&#034;)<br \/>\n                lines.append(&#034;) -&gt; str:\\\\n&#034;)<br \/>\n                lines.append(&#034;    import httpx\\\\n&#034;)<br \/>\n                lines.append(&#034;    async with httpx.AsyncClient() as client:\\\\n&#034;)<br \/>\n                lines.append(f&#034;        full_url &#061; f\\\\&#034;{url}{p}\\\\&#034;\\\\n&#034;)<\/p>\n<p>                # \u66ff\u6362\u8def\u5f84\u53c2\u6570<br \/>\n                for name, _ in _param_defs:<br \/>\n                    placeholder &#061; f&#034;{{{name}}}&#034;<br \/>\n                    if placeholder in p:<br \/>\n                        lines.append(f&#034;        full_url &#061; full_url.replace(&#039;{placeholder}&#039;, str({name}))\\\\n&#034;)<\/p>\n<p>                # \u6784\u5efa query \u53c2\u6570<br \/>\n                lines.append(&#034;        params &#061; {}\\\\n&#034;)<br \/>\n                for name, _ in _param_defs:<br \/>\n                    placeholder &#061; f&#034;{{{name}}}&#034;<br \/>\n                    if placeholder not in p:  # \u4e0d\u662f path \u53c2\u6570 \u2192 \u662f query<br \/>\n                        lines.append(f&#034;        if {name} is not None:\\\\n&#034;)<br \/>\n                        lines.append(f&#034;            params[&#039;{name}&#039;] &#061; {name}\\\\n&#034;)<\/p>\n<p>                # \u53d1\u9001\u8bf7\u6c42<br \/>\n                lines.append(f&#034;        method &#061; &#039;{m}&#039;.upper()\\\\n&#034;)<br \/>\n                if m.lower() &#061;&#061; &#034;get&#034;:<br \/>\n                    lines.append(&#034;        resp &#061; await client.get(full_url, params&#061;params)\\\\n&#034;)<br \/>\n                else:<br \/>\n                    if _has_body:<br \/>\n                        lines.append(<br \/>\n                            &#034;        resp &#061; await client.request(method, full_url, params&#061;params, json&#061;body)\\\\n&#034;)<br \/>\n                    else:<br \/>\n                        lines.append(&#034;        resp &#061; await client.request(method, full_url, params&#061;params)\\\\n&#034;)<br \/>\n                lines.append(&#034;        return resp.text\\\\n&#034;)<\/p>\n<p>                ns &#061; {}<br \/>\n                print(&#034;&#8212;&#8212;&#8212;&#8212;&#8212;-&#034;)<br \/>\n                print(&#034;&#034;.join(lines))<br \/>\n                print(&#034;&#8212;&#8212;&#8212;&#8212;&#8212;-&#034;)<br \/>\n                exec(&#034;&#034;.join(lines), ns, ns)<br \/>\n                return ns[&#034;tool_func&#034;]<\/p>\n<p>            tool &#061; make_tool(_param_defs&#061;param_defs, _has_body&#061;(body_example is not None))<br \/>\n            tool.__name__ &#061; operation_id<br \/>\n            tool.__doc__ &#061; dynamic_docstring<br \/>\n            mcp.tool(name&#061;operation_id, description&#061;dynamic_docstring)(tool)<\/p>\n<p>import argparse<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    parser &#061; argparse.ArgumentParser(description&#061;&#034;\u542f\u52a8 MCP \u670d\u52a1\u5668\u5e76\u6ce8\u518c OpenAPI \u5de5\u5177&#034;)<br \/>\n    parser.add_argument(<br \/>\n        &#034;&#8211;base-url&#034;,<br \/>\n        type&#061;str,<br \/>\n        required&#061;True,<br \/>\n        help&#061;&#034;API \u7684\u57fa\u7840 URL&#xff08;\u4f8b\u5982 http:\/\/127.0.0.1:8080&#xff09;&#034;<br \/>\n    )<br \/>\n    args &#061; parser.parse_args()<\/p>\n<p>    openapi_url &#061; f&#034;{args.base_url.rstrip(&#039;\/&#039;)}\/v3\/api-docs&#034;<\/p>\n<p>    print(&#034;Loading and resolving OpenAPI spec&#8230;&#034;)<br \/>\n    spec &#061; load_and_resolve_openapi_spec(openapi_url)<\/p>\n<p>    print(&#034;Registering tools&#8230;&#034;)<br \/>\n    register_openapi_tools(spec, args.base_url)<\/p>\n<p>    print(&#034;Starting MCP server on http:\/\/0.0.0.0:8000 (SSE transport)&#8230;&#034;)<br \/>\n    mcp.run(host&#061;&#034;0.0.0.0&#034;, port&#061;8000, transport&#061;&#034;sse&#034;) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x1f4dd; \u9879\u76ee\u4ecb\u7ecd (Project Description)<br \/>\n\u7b80\u4ecb<br \/>\nOpenAPI-to-MCP Bridge\u00a0\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 Python Web \u670d\u52a1\u5668&#xff0c;\u65e8\u5728\u6253\u7834\u4f20\u7edf\u540e\u7aef\u670d\u52a1\u4e0e\u5927\u8bed\u8a00\u6a21\u578b&#xff08;LLM&#xff09;Agent \u4e4b\u95f4\u7684\u58c1\u5792\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u62c9\u53d6\u73b0\u6709\u7684 Spring Boot \u5e94\u7528\u751f\u6210\u7684 OpenAPI (Swagger) \u89c4\u8303&#xff0c;\u5b9e\u65f6\u89e3\u6790\u63a5\u53e3\u5b9a\u4e49\u3001\u53c2\u6570\u7ea6\u675f\u53ca\u6570\u636e\u7ed3\u6784&#xff0c;\u5e76\u5c06\u5176\u52a8\u6001\u6ce8\u518c\u4e3a\u7b26\u5408\u00a0Model<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[73,98,292,43],"topic":[],"class_list":["post-82787","post","type-post","status-publish","format-standard","hentry","category-server","tag-mcp","tag-spring-boot","tag-292","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668 - \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\/82787.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"&#x1f4dd; \u9879\u76ee\u4ecb\u7ecd (Project Description) \u7b80\u4ecb OpenAPI-to-MCP Bridge\u00a0\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 Python Web \u670d\u52a1\u5668&#xff0c;\u65e8\u5728\u6253\u7834\u4f20\u7edf\u540e\u7aef\u670d\u52a1\u4e0e\u5927\u8bed\u8a00\u6a21\u578b&#xff08;LLM&#xff09;Agent \u4e4b\u95f4\u7684\u58c1\u5792\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u62c9\u53d6\u73b0\u6709\u7684 Spring Boot \u5e94\u7528\u751f\u6210\u7684 OpenAPI (Swagger) \u89c4\u8303&#xff0c;\u5b9e\u65f6\u89e3\u6790\u63a5\u53e3\u5b9a\u4e49\u3001\u53c2\u6570\u7ea6\u675f\u53ca\u6570\u636e\u7ed3\u6784&#xff0c;\u5e76\u5c06\u5176\u52a8\u6001\u6ce8\u518c\u4e3a\u7b26\u5408\u00a0Model\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/82787.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-25T04:49:19+00:00\" \/>\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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82787.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/82787.html\",\"name\":\"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-25T04:49:19+00:00\",\"dateModified\":\"2026-07-25T04:49:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/82787.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/82787.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82787.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668\"}]},{\"@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":"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668 - \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\/82787.html","og_locale":"zh_CN","og_type":"article","og_title":"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"&#x1f4dd; \u9879\u76ee\u4ecb\u7ecd (Project Description) \u7b80\u4ecb OpenAPI-to-MCP Bridge\u00a0\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 Python Web \u670d\u52a1\u5668&#xff0c;\u65e8\u5728\u6253\u7834\u4f20\u7edf\u540e\u7aef\u670d\u52a1\u4e0e\u5927\u8bed\u8a00\u6a21\u578b&#xff08;LLM&#xff09;Agent \u4e4b\u95f4\u7684\u58c1\u5792\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u62c9\u53d6\u73b0\u6709\u7684 Spring Boot \u5e94\u7528\u751f\u6210\u7684 OpenAPI (Swagger) \u89c4\u8303&#xff0c;\u5b9e\u65f6\u89e3\u6790\u63a5\u53e3\u5b9a\u4e49\u3001\u53c2\u6570\u7ea6\u675f\u53ca\u6570\u636e\u7ed3\u6784&#xff0c;\u5e76\u5c06\u5176\u52a8\u6001\u6ce8\u518c\u4e3a\u7b26\u5408\u00a0Model","og_url":"https:\/\/www.wsisp.com\/helps\/82787.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-25T04:49:19+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/82787.html","url":"https:\/\/www.wsisp.com\/helps\/82787.html","name":"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-25T04:49:19+00:00","dateModified":"2026-07-25T04:49:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/82787.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/82787.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/82787.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"OpenAPI-to-MCP Bridge: \u4e00\u952e\u5c06 Spring Boot OpenAPI \u8f6c\u6362\u4e3a MCP \u5de5\u5177\u670d\u52a1\u5668"}]},{"@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\/82787","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=82787"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82787\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=82787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=82787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=82787"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=82787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}