🚀 Elixir MCP 服務器
Elixir MCP 服務器為開發者提供了在 Elixir 環境中便捷搭建服務器的解決方案,支持多種框架配置,能滿足不同場景的開發需求。
🚀 快速開始
以下是一個簡單的示例,展示如何在 Elixir 中實現一個基本的 MCP 服務器:
require Logger
defmodule ExampleServer do
use MCP.Server
def handle_call(request_id, %{"method" => "initialize"} = _params) do
{:ok,
%{
jsonrpc: "2.0",
id: request_id,
result: :hello_world
}}
end
end
ExampleServer.start()
📦 安裝指南
要使用 MCP 服務器,請按照以下步驟安裝:
- 將項目添加到你的
mix.exs
文件中:def project do
[
app: :your_app,
version: "1.0",
elixir: "~> 1.12",
deps: [
{:mcp, "~> 0.9"}
]
]
end
- 下載依賴:
mix deps.get
💻 使用示例
基礎用法
使用 Phoenix 框架配置
在你的 Phoenix 應用中,添加以下管道和路由:
- 在
lib/my_app/pipeline.ex
中添加 MCP 管道:defmodule MyApp.Pipeline do
use Plug.Pipeline, otp_app: :my_app
plug :init_headers
plug Plug.Static, at: "/", from: {:my_app, "public"}
plug Plug.Router
plug MyApp.Router
plug MCP.PhoenixPipeline
end
- 在
lib/my_app/router.ex
中添加路由:defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :sse do
pipe_through [:phoenix_pipeline]
end
scope "/api" do
get "/mcp", MCP.PhoenixRouter, :index
post "/mcp/:endpoint", MCP.PhoenixRouter, :call
end
end
使用 Plug 框架配置
在你的 Plug 應用中,添加以下中間件和路由:
- 在
lib/my_app/middleware.ex
中添加 MCP 中間件:defmodule MyApp.Middleware do
use Plug.Router
plug :init
plug :my_router
def init(conn, _opts) do
conn
end
def my_router(conn, _) do
route(conn)
end
end
- 在
lib/my_app/router.ex
中添加路由:defmodule MyApp.Router do
use Plug.Router
get("/mcp", MCP.PlugRouter.init/1, MCP.PlugRouter.call/2)
post("/mcp/:endpoint", MCP.PlugRouter.init/1, MCP.PlugRouter.call/2)
end
📚 詳細文檔
配置選項
路徑自定義
你可以在配置文件中設置自定義路徑:
import_config "./envs/#{environment}.exs"
config :mcp, MCP.Router,
prefix: "/api",
endpoints: ["ping", "echo"]
保持 alive 超時
設置保持 alive 超時時間(以秒為單位):
config :mcp, MCP.Router,
keep_alive_timeout: 5000
⚠️ 注意事項
⚠️ 重要提示
確保你的服務器正確處理 HTTP/1.1 協議的保持 alive 機制。
💡 使用建議
在生產環境中,建議啟用 SSL 加密通信。
🤝 貢獻指南
歡迎貢獻!請按照以下步驟參與:
- 克隆倉庫:
git clone git@github.com:your-repo.git
cd your-repo
- 創建功能分支:
git checkout -b feature/your-feature
- 提交更改:
git add .
git commit -m "添加新功能"
git push origin feature/your-feature
- 創建拉取請求到主分支。
感謝你的貢獻!
📞 聯繫方式
如需幫助或反饋,請聯繫:
- 郵箱:support@example.com
- 社交媒體:GitHub
這個文檔提供了在 Elixir 中使用 MCP 服務器的基本配置和使用方法,希望對你有所幫助!