Skip to content

Commit a20efe6

Browse files
quinnjclaude
andcommitted
Give the static request context the request's concrete type
`StaticMCPRequestContext.request` was typed as the abstract `HTTP.Request`, so a tool handler lost the concrete type of the request the server received. Calls on it then depended on inference recovering the field types, and a trim-compiled app had to read the request with `getfield`. The context now takes the request type as a parameter. Building it and dispatching on `StaticMCPRequestContext` work as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3b4cf1c commit a20efe6

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎docs/src/static-server.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ namespace.
1414
Each tool handler must have the same concrete Julia type. One handler type can
1515
dispatch by tool name when a server has more than one tool. Tool arguments,
1616
schemas, annotations, and structured results use raw `JSON.JSONText` values.
17-
The handler must validate its arguments before it performs work.
17+
The handler must validate its arguments before it performs work. Its
18+
`context.request` is the request the server received, with its concrete type,
19+
so the handler can read the request's headers or target in a trimmed program.
1820

1921
```@example static-server
2022
using ModelContextProtocol

‎src/static_server.jl‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ StaticMCPToolResult(text, structured_content, is_error) =
2121
StaticMCPToolResult(text, structured_content, is_error, JSON.JSONText("{}"))
2222

2323
"""
24-
Request context passed to a static MCP tool handler. `session_id` is the
25-
request's `MCP-Session-Id` header, or empty when the request has none. A
26-
stateless server issues no session ids, so it is usually empty there.
24+
Request context passed to a static MCP tool handler. `request` is the request the
25+
server received, with its concrete type, so a handler can read it in a
26+
trim-compiled program. `session_id` is the request's `MCP-Session-Id` header, or
27+
empty when the request has none. A stateless server issues no session ids, so it
28+
is usually empty there.
2729
"""
28-
Base.@kwdef struct StaticMCPRequestContext
29-
request::HTTP.Request
30+
Base.@kwdef struct StaticMCPRequestContext{R<:HTTP.Request}
31+
request::R
3032
session_id::String
3133
end
3234

‎test/mcp_trim_safe.jl‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const JSON = ModelContextProtocol.JSON
1313
struct TrimEchoHandler end
1414

1515
function (::TrimEchoHandler)(
16-
::ModelContextProtocol.StaticMCPRequestContext,
16+
context::ModelContextProtocol.StaticMCPRequestContext,
1717
arguments::JSON.JSONText,
1818
)
19+
# Reading the request checks that it reaches the handler with a concrete type.
1920
return ModelContextProtocol.StaticMCPToolResult(
20-
text="echoed",
21+
text="echoed " * context.request.target,
2122
structured_content=arguments,
2223
meta=JSON.JSONText("{\"trace\":\"trim\"}"),
2324
)
@@ -121,7 +122,7 @@ function run_mcp_trim_sample()::Nothing
121122
)
122123
_trim_assert(called.status == 200, "tools/call status")
123124
called_body = _body(called)
124-
_trim_assert(occursin("\"text\":\"echoed\"", called_body), "tools/call text content")
125+
_trim_assert(occursin("\"text\":\"echoed /v1/mcp\"", called_body), "tools/call text read from the request")
125126
_trim_assert(occursin("\"structuredContent\":{\"message\":\"hi \\\"trim\\\"\"}", called_body), "tools/call structured content")
126127
_trim_assert(occursin("\"isError\":false", called_body), "tools/call success flag")
127128
_trim_assert(occursin("\"_meta\":{\"trace\":\"trim\"}", called_body), "tools/call result _meta")

‎test/runtests.jl‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ const STATIC_WIDGET_URI = "ui://static-test/card"
422422
# a closing script tag, and multi-byte Unicode including U+2028.
423423
const STATIC_WIDGET_HTML = "<!doctype html>\n<html><body data-x=\"1\">\n<script>const s = \"</script>\";\tback\\slash \u0001 é 🎾 
</script>\n</body></html>\n"
424424

425+
@testset "Static request context keeps the request type" begin
426+
request = static_raw_request("{}")
427+
context = ModelContextProtocol.StaticMCPRequestContext(request=request, session_id="")
428+
@test fieldtype(typeof(context), :request) === typeof(request)
429+
@test context.request === request
430+
end
431+
425432
struct StaticAppHandler
426433
kind::String
427434
end
@@ -434,7 +441,11 @@ function (handler::StaticAppHandler)(
434441
return ModelContextProtocol.StaticMCPToolResult(text="bad", meta=JSON.JSONText("[]"))
435442
return ModelContextProtocol.StaticMCPToolResult(
436443
text="card",
437-
structured_content=JSON.JSONText(JSON.json((; session_id=context.session_id, arguments=JSON.parse(arguments.value)))),
444+
structured_content=JSON.JSONText(JSON.json((;
445+
session_id=context.session_id,
446+
target=context.request.target,
447+
arguments=JSON.parse(arguments.value),
448+
))),
438449
meta=JSON.JSONText("{\"trace\":\"abc\"}"),
439450
)
440451
end
@@ -579,7 +590,8 @@ end
579590

580591
called = static_payload(call(static_body("tools/call"; params=Dict("name" => "card", "arguments" => Dict("id" => 7)))))["result"]
581592
@test called["_meta"] == Dict("trace" => "abc")
582-
@test called["structuredContent"] == Dict("session_id" => session_id, "arguments" => Dict("id" => 7))
593+
@test called["structuredContent"] ==
594+
Dict("session_id" => session_id, "target" => "/v1/mcp", "arguments" => Dict("id" => 7))
583595
@test only(called["content"])["text"] == "card"
584596
@test static_payload(call(static_body("tools/call"; params=Dict("name" => "bad-meta"))))["error"]["code"] == -32603
585597

0 commit comments

Comments
 (0)