iT邦幫忙

2025 iThome 鐵人賽

DAY 5
0
生成式 AI

MCP Server 全攻略:讓 AI 動態調用工具的工作流革命系列 第 5

[05] 還在手動寫 MCP Server?公開 API 一鍵轉換+自動 Deploy

  • 分享至 

  • xImage
  •  

前言

市面上已經有一些工具,可以幫助我們把 API 轉換成 MCP Server,讓 LLM 能直接使用。今天要介紹的,是其中一個相對新穎的免費工具,這是 huggingface 社群裡的 abidlabs 做的。

能夠一鍵轉換,自動幫你 Deploy 好 MCP Server。

不過要注意優缺點:

優點:快速、免手動部署。真的可以直接用!

缺點

所以最後只好先用比較簡單的資料來展示。

讓我們用官方 Swagger 提供的寵物商店範例來說明,一起和我玩玩看這個超讚工具吧

操作步驟

我們只需要兩個前置作業:

  • 註冊 huggingface 帳號

  • 選一個公開的 API

接著就可以開始了!
進到工具頁面: https://abidlabs-openapi2mcp.hf.space/?__theme=dark

https://ithelp.ithome.com.tw/upload/images/20250919/20141182UMr5QV4X6N.png

填寫資料

  • 輸入 API 文件地址 (例:https://petstore3.swagger.io/api/v3/openapi.json)
  • 填寫 base_url (例:https://petstore3.swagger.io) 注意!我這邊是故意填錯的,實際上是 https://petstore3.swagger.io/api/v3/,等一下示範怎麼修正!
  • 設定 Token (選填),如果 API 需要 Auth Token,只要展開「Optional Settings」,把 Token 貼上去即可。

https://ithelp.ithome.com.tw/upload/images/20250919/201411823MvAsv4zmQ.png

讓魔法開始

可以在產之前點「Find endpoint」看看右邊有沒有正確顯示 API,
確認沒問題後,就能點右下角 Create MCP Space讓魔法發生了!

這時候工具會自動幫你把 API 轉成 MCP Server,並且直接部署到雲端。

https://ithelp.ithome.com.tw/upload/images/20250919/201411828sOy0M0IbE.png
(右上角會跳出網址請點進去)

接著等狀態顯示綠燈 running,

https://ithelp.ithome.com.tw/upload/images/20250919/20141182w4sFAhKx8r.png

https://ithelp.ithome.com.tw/upload/images/20250919/20141182Aqfggw6ycv.png

成功後就會出現類似這樣的畫面:

https://ithelp.ithome.com.tw/upload/images/20250919/20141182W4jLJkEM2J.png

往下滑到頁面底部,點選 Use via API or MCP,就能看到 MCP Server 的設定方式。

https://ithelp.ithome.com.tw/upload/images/20250919/20141182Vzxn7hkGn5.png

https://ithelp.ithome.com.tw/upload/images/20250919/20141182SxJul4ui2x.png

點開後,會看到跟Mcp Server 有關的資訊,我們再往下滑找到 json 檔的設定方式:

https://ithelp.ithome.com.tw/upload/images/20250919/20141182Dep5IbfkDK.png

https://ithelp.ithome.com.tw/upload/images/20250919/201411821ICumUq5Yo.png

它甚至已經幫你把 設定 JSON 產生好了,只要複製起來,貼到你的 Host(這裡以 Cursor 為例)就行。

新增 Mcp Server 到你的 host (cursor為例)

不熟的夥伴幫我看 [02] 零基礎新手體驗:如何在 Cursor 玩 MCP Server(Notion 為例)

會的夥伴我們繼續做,

打開 Cursor → 右上角設定的齒輪icon

點進MCP Server 頁面

https://ithelp.ithome.com.tw/upload/images/20250919/20141182HiaVOELwPK.png

新增一個 MCP Server

https://ithelp.ithome.com.tw/upload/images/20250919/20141182Uxz37c2gGh.png

把剛剛複製的 JSON 設定貼上

回到頁面,就能看到正確連線!

https://ithelp.ithome.com.tw/upload/images/20250919/20141182OxWKV9JrUQ.png

這個 MCP Server 是部署在雲端,所以你的朋友也能直接使用,這就是我們在 [03] 如何讓 MCP Server 與 Client 溝通:STDIO 與 Streamable HTTP 提到的 Streamable HTTP 介紹的 Streamable HTTP Mcp Server,

它就不會像[02] 零基礎新手體驗:如何在 Cursor 玩 MCP Server(Notion 為例)玩的 STDIO Mcp Server 一樣只有自己能玩。

測試

好啦! Mcp Server 設定好了,我們來嘗試請 LLM 幫我們查一下目前有多少寵物庫存,

請問目前有多少寵物庫存

https://ithelp.ithome.com.tw/upload/images/20250919/20141182BjwskbTsfO.png

可以看到失敗了,LLM 試著調用工具,結果收到 404。

這就是我一開始埋的伏筆,我故意填錯了 base_url,所以打 API 當然會失敗 (404)。

這時候只要修正 base_url 就好。

修改 Mcp Server 的 Code

那怎麼修正呢?不需要重新回到一開始的步驟從頭開始做,這就是有趣的地方了,這個工具不僅能幫你產生程式碼,還支援像 GitLab 一樣的介面,讓你可以修改程式碼、直接發 Commit。

我們點右上方的 Files

https://ithelp.ithome.com.tw/upload/images/20250919/20141182Jpi82T1y2p.png

就會進到像 gitlab 的頁面,這時候點開 app.py

https://ithelp.ithome.com.tw/upload/images/20250919/20141182pudyb8vAJl.png

看到了嗎,是不是很眼熟呢?

我們直接來改 base_url 的數值成正確的 https://petstore3.swagger.io/api/v3/

before:

https://ithelp.ithome.com.tw/upload/images/20250919/20141182e1JY4k6Nbk.png

gr.load_openapi(
    openapi_spec="https://petstore3.swagger.io/api/v3/openapi.json",
    base_url="https://petstore3.swagger.io/",
    paths=None,
    methods=['POST', 'GET'],
    auth_token=os.getenv("OPENAPI_AUTH_TOKEN")
).launch(mcp_server=True)

after:

https://ithelp.ithome.com.tw/upload/images/20250919/20141182vaPLm1YHns.png

gr.load_openapi(
    openapi_spec="https://petstore3.swagger.io/api/v3/openapi.json",
    base_url="https://petstore3.swagger.io/api/v3",
    paths=None,
    methods=['POST', 'GET'],
    auth_token=os.getenv("OPENAPI_AUTH_TOKEN")
).launch(mcp_server=True)

改完之後,它還會自動重新 Deploy,真的非常方便。

https://ithelp.ithome.com.tw/upload/images/20250919/20141182ONIuvc6PwH.png

等重新部署完成,再回到 Cursor 下剛剛的指令,就能正確使用了,大功告成!恭喜你學會了

https://ithelp.ithome.com.tw/upload/images/20250919/20141182duPqPDXdnc.png

https://ithelp.ithome.com.tw/upload/images/20250919/20141182lfo66FTRf5.png

我們明天會再介紹其他把 api文件 轉換成 Mcp Server的工具!希望你也覺得很酷!


上一篇
[04] RPC-JSON 速讀指南
下一篇
[06] Postman MCP Generator
系列文
MCP Server 全攻略:讓 AI 動態調用工具的工作流革命12
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言