README
🚀 Elixir MCP 服务器
Elixir MCP 服务器为开发者提供了在 Elixir 环境中便捷搭建服务器的解决方案,支持多种框架配置,能满足不同场景的开发需求。
🚀 快速开始
以下是一个简单的示例,展示如何在 Elixir 中实现一个基本的 MCP 服务器:
# dev/example_server.exs
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
📚 详细文档
配置选项
路径自定义
你可以在配置文件中设置自定义路径:
# config/config.exs
import_config "./envs/#{environment}.exs"
config :mcp, MCP.Router,
prefix: "/api",
endpoints: ["ping", "echo"]
保持 alive 超时
设置保持 alive 超时时间(以秒为单位):
# config/config.exs
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 服务器的基本配置和使用方法,希望对你有所帮助!
Scan to contact