ClaudeCode.MCP.Config (ClaudeCode v0.16.0)
View SourceGenerates MCP configuration files for the Claude CLI.
The Claude CLI expects MCP server configurations in a specific JSON format. This module provides helpers to generate these configuration files for different transport types (HTTP/SSE and stdio).
Configuration Format
The Claude CLI expects a JSON file with the following structure:
{
"mcpServers": {
"server-name": {
"command": "path/to/executable",
"args": ["arg1", "arg2"],
"env": {"KEY": "value"}
}
}
}For HTTP/SSE transport:
{
"mcpServers": {
"server-name": {
"url": "http://localhost:9001/sse"
}
}
}Usage
# Generate HTTP config
config = ClaudeCode.MCP.Config.http_config("my-server", port: 9001)
# Generate stdio config
config = ClaudeCode.MCP.Config.stdio_config("my-server",
command: "elixir",
args: ["-S", "mix", "run", "--no-halt", "-e", "MyApp.MCPServer.start_link()"]
)
# Write to temp file for Claude CLI
{:ok, path} = ClaudeCode.MCP.Config.write_temp_config(config)
# Use with ClaudeCode session
{:ok, session} = ClaudeCode.start_link(mcp_config: path)
Summary
Functions
Generates an HTTP/SSE transport configuration for an MCP server.
Merges multiple MCP configurations into a single configuration.
Generates a stdio transport configuration for an MCP server.
Converts an MCP configuration to a JSON string.
Writes an MCP configuration to a temporary file.
Types
Functions
@spec http_config( String.t(), keyword() ) :: mcp_config()
Generates an HTTP/SSE transport configuration for an MCP server.
Options
:port- Port number (required):host- Hostname (default: "localhost"):path- SSE endpoint path (default: "/sse"):scheme- URL scheme (default: "http")
Example
config = ClaudeCode.MCP.Config.http_config("calculator", port: 9001)
# => %{mcpServers: %{"calculator" => %{url: "http://localhost:9001/sse"}}}
config = ClaudeCode.MCP.Config.http_config("secure-server",
port: 443,
host: "api.example.com",
scheme: "https",
path: "/mcp/sse"
)
@spec merge_configs([mcp_config()]) :: mcp_config()
Merges multiple MCP configurations into a single configuration.
This is useful when you have multiple MCP servers that should all be available to Claude.
Example
calculator = ClaudeCode.MCP.Config.http_config("calculator", port: 9001)
database = ClaudeCode.MCP.Config.http_config("database", port: 9002)
merged = ClaudeCode.MCP.Config.merge_configs([calculator, database])
# => %{mcpServers: %{"calculator" => ..., "database" => ...}}
@spec stdio_config( String.t(), keyword() ) :: mcp_config()
Generates a stdio transport configuration for an MCP server.
Options
:command- Executable command (required):args- List of command arguments (default: []):env- Environment variables map (default: %{})
Example
config = ClaudeCode.MCP.Config.stdio_config("my-tools",
command: "elixir",
args: ["-S", "mix", "run", "--no-halt", "-e", "MyApp.start_mcp()"]
)
config = ClaudeCode.MCP.Config.stdio_config("node-server",
command: "npx",
args: ["@example/mcp-server"],
env: %{"API_KEY" => "secret"}
)
@spec to_json( mcp_config(), keyword() ) :: {:ok, String.t()} | {:error, term()}
Converts an MCP configuration to a JSON string.
Options
:pretty- Format with indentation (default: false)
Example
config = ClaudeCode.MCP.Config.http_config("my-server", port: 9001)
{:ok, json} = ClaudeCode.MCP.Config.to_json(config, pretty: true)
@spec write_temp_config( mcp_config(), keyword() ) :: {:ok, String.t()} | {:error, term()}
Writes an MCP configuration to a temporary file.
Returns the path to the temporary file, which can be passed to
ClaudeCode via the :mcp_config option.
The temporary file is created in the system's temp directory and will be automatically cleaned up by the OS eventually.
Options
:prefix- Filename prefix (default: "claude_mcp_config"):dir- Directory for temp file (default: System.tmp_dir!())
Example
config = ClaudeCode.MCP.Config.http_config("my-server", port: 9001)
{:ok, path} = ClaudeCode.MCP.Config.write_temp_config(config)
{:ok, session} = ClaudeCode.start_link(mcp_config: path)