This is an Elixir library that implements the Model Context Protocol (MCP) server specification using Plug. It provides a complete MCP server implementation with JSON-RPC 2.0 support, SSE streaming, session management, and security features. See @README.md
mix deps.get- Install dependenciesmix deps.compile- Compile dependencies
mix test- Run all testsmix test test/specific_test.exs- Run a specific test filemix test --trace- Run tests with detailed output
mix format- Format code according to Elixir style guidelines.mix compile --warnings-as-errors- Compile with strict warnings
mix docs- Generate documentation with ExDoc.h {Module}- Lookup documentation in IEX for the module.h {Module.function}- Lookup documentation in IEX for a function
When getting documentation on an Elixir function, lookup both the module and the function to get more context on its appropriate usage.
iex -S mix- Start a server and IEX session
The library is organized into several key modules:
Phantom.Request- JSON-RPC 2.0 message parsing, encoding, and validationPhantom.Cache- Manages:persistent_termthat loads available MCP tools, prompts, and resources defined at compile time and added or removed at runtime.Phantom.Session- Session management with timeouts and data storagePhantom.Router- Behaviour for implementing MCP methodsPhantom.Plug- Main Plug implementation for HTTP transport with SSE support
- Comprehensive test coverage for all modules
- Integration tests for HTTP requests including stateful SSE connections with sessions
- Handler behavior testing
- Protocol compliance verification
Session Management is optional and should use Elixir's Supervisor and have each
MCP session be its own GenServer supervised by a DynamicSupervisor. Elixir can
have many remote nodes with MCP Sessions on another node, so use Phoenix.Tracker
from the hex package phoenix_pubsub to allow Session management to work across
nodes transparently. It should be inspired by Phoenix.Socket which can be found
with the Tidewave MCP tool project_eval with h Phoenix.Socket.
The Handler provides an interface like Plug.Router with new macros that compiles
tools, resources, and prompts that are available, however these should be
configurable in runtime as well. The syntax should be inspired by
Ecto.Schema which can be found with the Tidewave MCP tool project_eval with
h Ecto.Schema.
The MCP.Server is optional and designed for extending non-web applications to be exposed via a slim Bandit and Plug server. Normal usage of Phantom should be through Phoenix applications that already have a web server.
The MCP.Console is optional and designed for using the MCP server locally only.
You write code using Test-Driven-Development method, which means you write the tests first, run the tests to verify they fail,
and then write the code to make the tests pass. Finally you run mix format
when everything is done.
- MCP Specification Compliance: Full JSON-RPC 2.0 with streamable HTTP transport
- Plug Integration: First-class Plug support
- Security: Origin validation, CORS handling, localhost binding, session management
- Performance: Bandit HTTP server, efficient message handling
- Configuration: Application config, environment-specific settings, runtime configuration
- Testing: Complete test suite with Phoenix integration tests
- Phantom.Plug: Dedicated Plug with CORS and authentication support
- Multiple Integration Patterns: Standalone server, Phoenix Endpoint integration
- Security: Origin validation, remote access control, preflight request handling