Back to MCP directory
publicPublicdnsLocal runtime

mcp_sse

MCP over SSE是一个基于Server-Sent Events实现Model Context Protocol的开源库,提供了完整的MCP服务器功能,包括SSE连接管理、JSON-RPC消息处理、工具注册执行和会话管理等功能。

article

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 服务器,请按照以下步骤安装:

  1. 将项目添加到你的 mix.exs 文件中:
    def project do
      [
        app: :your_app,
        version: "1.0",
        elixir: "~> 1.12",
        deps: [
          {:mcp, "~> 0.9"}
        ]
      ]
    end
    
  2. 下载依赖:
    mix deps.get
    

💻 使用示例

基础用法

使用 Phoenix 框架配置

在你的 Phoenix 应用中,添加以下管道和路由:

  1. 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
    
  2. 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 应用中,添加以下中间件和路由:

  1. 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
    
  2. 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 加密通信。

🤝 贡献指南

欢迎贡献!请按照以下步骤参与:

  1. 克隆仓库:
    git clone git@github.com:your-repo.git
    cd your-repo
    
  2. 创建功能分支:
    git checkout -b feature/your-feature
    
  3. 提交更改:
    git add .
    git commit -m "添加新功能"
    git push origin feature/your-feature
    
  4. 创建拉取请求到主分支。

感谢你的贡献!

📞 联系方式

如需帮助或反馈,请联系:

  • 邮箱:support@example.com
  • 社交媒体:GitHub

这个文档提供了在 Elixir 中使用 MCP 服务器的基本配置和使用方法,希望对你有所帮助!

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client