Julia server and client utilities for the Model Context Protocol (MCP).
The package supports the stateful MCP 2025-11-25 protocol and the stateless
MCP 2026-07-28 protocol over Streamable HTTP. Clients default to
2025-11-25 for compatibility. The package includes discovery manifests,
OAuth-protected resources, tools, prompts, resources, completions, logging,
multi-round-trip results, subscriptions, and custom tool headers. It also
ships first-class support for MCP Apps (SEP-1865) —
interactive HTML widgets rendered inline by hosts like Cursor and Claude; see
MCP-App-playbook.md for the end-to-end guide.
This JuliaServices package is currently unregistered. The General registry has
a separate package with the same name, so Pkg.add("ModelContextProtocol")
does not install this checkout.
Use a URL or local development checkout instead:
using Pkg
Pkg.develop(url="https://github.com/JuliaServices/ModelContextProtocol.jl")using ModelContextProtocol
server = MCPServer(MCPServerConfig(name="Calculator", version="0.1.0"))
register_tool!(
server;
name="add",
description="Add numbers.",
input_schema=Dict(
"type" => "object",
"properties" => Dict(
"numbers" => Dict("type" => "array", "items" => Dict("type" => "number")),
),
"required" => ["numbers"],
),
handler=function (_context, args)
total = sum(Float64.(args["numbers"]))
return Dict(
"content" => [Dict("type" => "text", "text" => string(total))],
"structuredContent" => Dict("sum" => total),
)
end,
)
http_server = serve_mcp_http(server; host="127.0.0.1", port=3010)
wait(http_server.http)StaticMCPServer is a specialized server with a concrete request graph for
JuliaC --trim=safe builds. It serves tools, fixed resources, and
MCP Apps widgets. Its API is namespaced and is not
exported. This tool renders a widget in hosts that support MCP Apps:
using ModelContextProtocol
const MCP = ModelContextProtocol
struct CardHandler end
function (::CardHandler)(::MCP.StaticMCPRequestContext, arguments::MCP.JSON.JSONText)
return MCP.StaticMCPToolResult(
text="Q3 revenue report",
structured_content=MCP.JSON.JSONText("{\"title\":\"Q3 revenue\"}"),
)
end
# Build the HTML at load time; mcp_app_html is not part of the trim-checked path.
const CARD_HTML = MCP.mcp_app_html(
app_name="acme-card",
body="<main id=\"app\"></main>",
script="mcpApp.onRender(function (data) { document.getElementById('app').textContent = data.title; });",
)
card = MCP.StaticMCPResource(
uri="ui://acme/card",
name="acme-card",
mime_type=MCP.MCP_APP_HTML_MIME_TYPE,
text=CARD_HTML,
)
server = MCP.StaticMCPServer(
[MCP.StaticMCPTool(
name="show-report",
description="Show the Q3 revenue report.",
meta=MCP.static_ui_tool_meta(card.uri),
handler=CardHandler(),
)];
name="Acme",
version="1.0.0",
resources=[card],
extensions=MCP.static_ui_extensions(),
stateless=true, # no sessions, so a restarted or second machine serves any client
)
router = MCP.HTTP.Router()
MCP.HTTP.register!(router, "POST", "/v1/mcp", req -> MCP.handle_static_jsonrpc_request(server, req))
MCP.HTTP.register!(router, "GET", "/v1/mcp", req -> MCP.handle_static_stream_request(server, req))
MCP.HTTP.register!(router, "DELETE", "/v1/mcp", req -> MCP.handle_static_session_delete(server, req))See the trim-safe static server guide for protocol versions, sessions, and support limits.
using ModelContextProtocol
discovery = discover_server("http://127.0.0.1:3010")
client = prepare_manual_client(discovery)
initialize_client!(client)
result = call_tool(client, "add"; arguments=Dict("numbers" => [1, 3, 4]))
terminate_session!(client)Use the stateless protocol when the server supports MCP 2026-07-28:
client = prepare_manual_client(
discovery;
config=MCPClientConfig(
protocol_version=ModelContextProtocol.PROTOCOL_VERSION_2026_07_28,
),
capabilities=Dict("elicitation" => Dict()),
)
initialize_client!(client) # calls server/discoverSee the MCP 2026-07-28 guide for capability checks, multi-round-trip results, custom headers, caching, and subscriptions.
The experimental MCP Events webhook profile adds
authenticated event discovery and subscriptions, signed HTTPS callbacks,
restart-safe subscription storage, and namespaced client helpers. Events use
MCP 2026-07-28; polling, push streams, and replay are not advertised.
When Agentif.jl is loaded, this package can register Agentif tools directly:
using Agentif
using ModelContextProtocol
tool = @tool "Echo text." echo(text::String) = text
server = MCPServer(MCPServerConfig(name="Agentif MCP", version="0.1.0"))
register_tool!(server, tool)See examples/ for calculator servers with and without OAuth. The docs include
a deeper walkthrough of the Auth0-federated calculator example.