Skip to content

Commit 7cfdc94

Browse files
authored
Fix ChatGPT event discovery for portable schemas (#21)
* Fix ChatGPT event discovery for portable schemas Preserve runtime validation and explicit legacy dialects while publishing portable implicit schemas with the MCP default. Prepare version 1.0.1. * Precompile the JuliaC test environment during setup * Use the host CPU target for trim test subprocesses * Cover portable event schema constraints and annotations
1 parent 0475416 commit 7cfdc94

6 files changed

Lines changed: 209 additions & 13 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ModelContextProtocol"
22
uuid = "a630a0b0-4325-47f6-86f8-c7db38e4394e"
3-
version = "1.0.0"
3+
version = "1.0.1"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

‎ROADMAP.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ The repository tests these areas:
7575
API. The separate static server remains the supported native subset.
7676
- Tool input and output schemas are advertised but are not a complete runtime
7777
JSON Schema validation engine. A handler must still validate domain rules.
78-
- Event schemas use JSONSchema.jl drafts 4, 6, and 7. Delivery is webhook-only
78+
- Event validation uses JSONSchema.jl drafts 4, 6, and 7. Portable implicit
79+
schemas are advertised without a dialect for MCP's default 2020-12; explicit
80+
dialects and other implicit schemas retain their legacy declarations.
81+
This is not a general 2020-12 validator. Delivery is webhook-only
7982
and synchronous. No polling, push, replay, dynamic catalog notifications,
8083
durable outbox, or gap/termination envelopes are implemented. Subscriptions
8184
always have finite lifetimes.

‎docs/src/events.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,18 @@ it explicitly; the package cannot infer whether an argument is a filter, glob,
8585
or transformation. Optional `transform(subject, arguments, data)` can redact
8686
each subscriber's payload. Hooks receive independent copies. JSONSchema.jl
8787
validates subscription arguments and delivered payloads using drafts 4, 6,
88-
and 7. An omitted dialect is explicitly published as draft 7; it never implies
89-
2020-12 validation. Newer dialects and external schema references are rejected.
88+
and 7. When an implicit schema uses only portable keywords, the catalog leaves
89+
out `$schema`, so clients use MCP's default 2020-12 dialect. Local validation
90+
retains the same assertions. This allows ChatGPT's event settings discovery to
91+
accept ordinary object schemas without requiring optional draft-7 support.
92+
93+
Portable schemas include scalar constraints, object properties and required
94+
fields, homogeneous array items, and common composition keywords. Explicit
95+
dialects are always preserved. Implicit schemas with references, tuple items,
96+
`dependencies`, `format`, or unrecognized keywords retain an explicit draft-7
97+
declaration. Such schemas require a client that supports that dialect. This
98+
publication rule does not add general 2020-12 validation: newer explicit
99+
dialects and external schema references are rejected.
90100
Only local JSON Pointer references are resolved. Domain rules remain your
91101
application's responsibility.
92102

‎src/events.jl‎

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,65 @@ function check_event_schema_references(schema)
304304
return nothing
305305
end
306306

307+
# Only omit the dialect when draft 7 and MCP's default 2020-12 have the same
308+
# assertions. Unknown keywords retain the existing explicit draft-7 contract.
309+
function portable_event_schema(schema)
310+
schema isa Bool && return true
311+
schema isa AbstractDict || return false
312+
for (key, value) in schema
313+
if key in ("properties", "patternProperties")
314+
value isa AbstractDict && all(portable_event_schema, values(value)) || return false
315+
elseif key in ("items", "additionalProperties", "propertyNames", "not", "if", "then", "else")
316+
portable_event_schema(value) || return false
317+
elseif key in ("allOf", "anyOf", "oneOf")
318+
value isa AbstractVector && !isempty(value) && all(portable_event_schema, value) || return false
319+
elseif key == "type"
320+
types = value isa AbstractString ? (value,) : value
321+
types isa Union{Tuple,AbstractVector} && !isempty(types) &&
322+
all(type -> type in ("null", "boolean", "object", "array", "number", "string", "integer"), types) || return false
323+
elseif key == "required"
324+
value isa AbstractVector && all(item -> item isa AbstractString, value) || return false
325+
elseif key in ("minLength", "maxLength", "minItems", "maxItems", "minProperties", "maxProperties")
326+
value isa Real && !(value isa Bool) && isfinite(value) && isinteger(value) && value >= 0 || return false
327+
elseif key in ("minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf")
328+
value isa Real && !(value isa Bool) && isfinite(value) || return false
329+
key == "multipleOf" && value <= 0 && return false
330+
elseif key == "uniqueItems"
331+
value isa Bool || return false
332+
elseif key in ("pattern", "title", "description", "\$comment")
333+
value isa AbstractString || return false
334+
elseif key in ("readOnly", "writeOnly")
335+
value isa Bool || return false
336+
elseif key == "enum"
337+
value isa AbstractVector && !isempty(value) || return false
338+
elseif key == "examples"
339+
value isa AbstractVector || return false
340+
elseif key in ("const", "default")
341+
# Literal JSON values are data, not nested schemas.
342+
else
343+
return false
344+
end
345+
end
346+
return true
347+
end
348+
349+
function event_schema_validator!(schema)
350+
portable = portable_event_schema(schema)
351+
get!(schema, "\$schema", "http://json-schema.org/draft-07/schema#")
352+
schema["\$schema"] isa AbstractString || throw(ArgumentError("Event schema dialect must be a string"))
353+
check_event_schema_references(schema)
354+
validator = JSONSchema.Schema(deepcopy(schema))
355+
portable && delete!(schema, "\$schema")
356+
return validator
357+
end
358+
307359
"""
308360
register_event!(server; name, input_schema, payload_schema, matches, ...)
309361
310362
Register an emit-only webhook event. Schemas are validated by JSONSchema.jl
311-
(draft 4, 6, or 7); omitted dialects are explicitly published as draft 7.
363+
(draft 4, 6, or 7). Implicit schemas using only portable keywords are published
364+
without a dialect, using MCP's default 2020-12. Other implicit schemas are
365+
explicitly published as draft 7; explicit supported dialects are preserved.
312366
External schema references are rejected. matches(principal, arguments, data) applies subscription
313367
filters; transform(principal, arguments, data) may redact the delivered payload.
314368
The catalog is configured at startup. Duplicate names are rejected; publish
@@ -322,14 +376,11 @@ function register_event!(server::MCPServer; name, input_schema, payload_schema,
322376
event_name = event_string(name, "name"; max_bytes=256)
323377
input = event_object(input_schema, "input_schema")
324378
payload = event_object(payload_schema, "payload_schema")
325-
for schema in (input, payload)
326-
get!(schema, "\$schema", "http://json-schema.org/draft-07/schema#")
327-
schema["\$schema"] isa AbstractString || throw(ArgumentError("Event schema dialect must be a string"))
328-
check_event_schema_references(schema)
329-
end
379+
input_validator = event_schema_validator!(input)
380+
payload_validator = event_schema_validator!(payload)
330381
definition = MCPServerEvent(
331382
event_name, description === nothing ? nothing : String(description),
332-
input, payload, JSONSchema.Schema(deepcopy(input)), JSONSchema.Schema(deepcopy(payload)),
383+
input, payload, input_validator, payload_validator,
333384
matches, transform, event_object(meta, "meta"),
334385
)
335386
@lock events.lock begin

‎test/events.jl‎

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ event_records(fixture) = EventsMCP.event_subscriptions(fixture.server.events.sto
108108
@test descriptor["delivery"] == ["webhook"]
109109
@test descriptor["inputSchema"]["required"] == ["resource"]
110110
@test descriptor["payloadSchema"]["required"] == ["resource", "text"]
111-
@test descriptor["inputSchema"]["\$schema"] == "http://json-schema.org/draft-07/schema#"
111+
@test !haskey(descriptor["inputSchema"], "\$schema")
112+
@test !haskey(descriptor["payloadSchema"], "\$schema")
112113
_, page = event_request(server, "events/list", Dict("limit" => 1); owner="beta")
113114
@test page["result"]["nextCursor"] == "2"
114115
_, next = event_request(server, "events/list", Dict("cursor" => page["result"]["nextCursor"]); owner="beta")
@@ -152,6 +153,131 @@ event_records(fixture) = EventsMCP.event_subscriptions(fixture.server.events.sto
152153
@test !haskey(local_schema, "\$schema")
153154
end
154155

156+
@testset "Portable event schema publication preserves validation" begin
157+
fixture = event_fixture()
158+
server = fixture.server
159+
schema = Dict{String,Any}("type" => "object", "properties" => Dict(
160+
"label" => Dict("type" => ["string", "null"]),
161+
"tags" => Dict("type" => "array", "items" => Dict("type" => "string")),
162+
), "additionalProperties" => false)
163+
original = deepcopy(schema)
164+
EventsMCP.register_event!(server; name="portable", input_schema=schema, payload_schema=schema,
165+
matches=(_...) -> true)
166+
definition = server.events.definitions["portable"]
167+
@test EventsMCP.event_descriptor(definition)["inputSchema"] == original
168+
@test EventsMCP.event_descriptor(definition)["payloadSchema"] == original
169+
@test schema == original
170+
for validator in (definition.input_validator, definition.payload_validator)
171+
@test isvalid(validator, Dict("label" => nothing, "tags" => ["one"]))
172+
@test !isvalid(validator, Dict("label" => 1))
173+
@test !isvalid(validator, Dict("tags" => [1]))
174+
@test !isvalid(validator, Dict("unexpected" => true))
175+
end
176+
response, rejected = event_request(server, "events/subscribe",
177+
event_params(name="portable", arguments=Dict("label" => 1)))
178+
@test response.status == 200
179+
@test rejected["error"]["code"] == -32602
180+
@test isempty(fixture.calls)
181+
@test isempty(event_records(fixture))
182+
@test_throws ArgumentError EventsMCP.emit_event!(server, "portable", Dict("label" => 1))
183+
@test isempty(fixture.calls)
184+
185+
# Property names and literal values are not schema keyword positions.
186+
literals = Dict{String,Any}("type" => "object", "properties" => Dict(
187+
"\$schema" => Dict("enum" => [Dict("\$schema" => "literal", "\$ref" => "literal")]),
188+
))
189+
EventsMCP.register_event!(server; name="literals", input_schema=literals, payload_schema=Dict(), matches=(_...) -> true)
190+
@test EventsMCP.event_descriptor(server.events.definitions["literals"])["inputSchema"] == literals
191+
192+
for dialect in ("http://json-schema.org/draft-04/schema#", "https://json-schema.org/draft-06/schema", "http://json-schema.org/draft-07/schema#")
193+
declared = merge(deepcopy(schema), Dict("\$schema" => dialect))
194+
EventsMCP.register_event!(server; name=dialect, input_schema=declared, payload_schema=Dict(), matches=(_...) -> true)
195+
@test EventsMCP.event_descriptor(server.events.definitions[dialect])["inputSchema"] == declared
196+
end
197+
198+
legacy_schemas = [
199+
Dict("type" => "array", "items" => [Dict("type" => "string")], "additionalItems" => false),
200+
Dict("dependencies" => Dict("a" => ["b"])),
201+
Dict("definitions" => Dict("text" => Dict("type" => "string")), "\$ref" => "#/definitions/text", "maxLength" => 1),
202+
Dict("properties" => Dict("nested" => Dict("type" => "array", "items" => [Dict("type" => "integer")]))),
203+
Dict("properties" => Dict("nested" => Dict("\$schema" => "http://json-schema.org/draft-07/schema#", "type" => "string"))),
204+
Dict("format" => "email"),
205+
Dict("prefixItems" => [Dict("type" => "string")]),
206+
Dict("unevaluatedProperties" => false),
207+
Dict("dependentRequired" => Dict("a" => ["b"])),
208+
Dict("contains" => Dict("type" => "integer"), "minContains" => 2),
209+
Dict("x-custom" => Dict("type" => "string")),
210+
Dict("exclusiveMinimum" => true, "minimum" => 1),
211+
]
212+
for (index, legacy) in enumerate(legacy_schemas)
213+
name = "legacy-$(index)"
214+
EventsMCP.register_event!(server; name, input_schema=legacy, payload_schema=schema, matches=(_...) -> true)
215+
descriptor = EventsMCP.event_descriptor(server.events.definitions[name])
216+
@test descriptor["inputSchema"] == merge(legacy, Dict("\$schema" => "http://json-schema.org/draft-07/schema#"))
217+
@test descriptor["payloadSchema"] == original
218+
@test !haskey(legacy, "\$schema")
219+
end
220+
tuple_validator = server.events.definitions["legacy-1"].input_validator
221+
@test isvalid(tuple_validator, ["one"])
222+
@test !isvalid(tuple_validator, [1])
223+
@test !isvalid(tuple_validator, ["one", "two"])
224+
dependency_validator = server.events.definitions["legacy-2"].input_validator
225+
@test !isvalid(dependency_validator, Dict("a" => true))
226+
@test isvalid(dependency_validator, Dict("a" => true, "b" => false))
227+
@test isvalid(server.events.definitions["legacy-3"].input_validator, "long text")
228+
@test_throws ArgumentError EventsMCP.register_event!(server; name="annotation-ref",
229+
input_schema=Dict("default" => Dict("\$ref" => "https://127.0.0.1/private")), payload_schema=schema, matches=(_...) -> true)
230+
end
231+
232+
@testset "Portable event constraints keep their assertions" begin
233+
cases = [
234+
(Dict("type" => "number", "minimum" => 0, "maximum" => 10,
235+
"exclusiveMinimum" => 0, "exclusiveMaximum" => 10, "multipleOf" => 0.5),
236+
[1, 9.5], [0, 10, 1.25]),
237+
(Dict{String,Any}("type" => "string", "minLength" => 2, "maxLength" => 4,
238+
"pattern" => "^[a-z]+\$", "title" => "Label", "description" => "A short label",
239+
"\$comment" => "Lowercase labels only", "readOnly" => false, "writeOnly" => true,
240+
"default" => "ok", "examples" => ["ok"]),
241+
["ok", "good"], ["x", "longer", "UP"]),
242+
(Dict("type" => "array", "items" => Dict("type" => "string"),
243+
"minItems" => 1, "maxItems" => 2, "uniqueItems" => true),
244+
[["one"], ["one", "two"]], [String[], ["one", "one"], ["one", "two", "three"], [1]]),
245+
(Dict{String,Any}("type" => "object", "minProperties" => 1, "maxProperties" => 2,
246+
"propertyNames" => Dict("minLength" => 1),
247+
"patternProperties" => Dict("^x_" => Dict("type" => "boolean"))),
248+
[Dict("x_a" => true), Dict("other" => 1)],
249+
[Dict(), Dict("" => 1), Dict("x_a" => 1), Dict("a" => 1, "b" => 2, "c" => 3)]),
250+
(Dict("allOf" => [
251+
Dict("anyOf" => [Dict("const" => "a"), Dict("const" => "bb")]),
252+
Dict("oneOf" => [Dict("type" => "string"), Dict("type" => "number")]),
253+
Dict("not" => Dict("const" => "cc")),
254+
Dict("if" => Dict("const" => "a"), "then" => Dict("maxLength" => 1),
255+
"else" => Dict("minLength" => 2)),
256+
true,
257+
]), ["a", "bb"], ["b", "cc", 1]),
258+
]
259+
fixture = event_fixture()
260+
for (index, (schema, accepted, rejected)) in enumerate(cases)
261+
name = "constraints-$(index)"
262+
original = deepcopy(schema)
263+
EventsMCP.register_event!(fixture.server; name, input_schema=schema,
264+
payload_schema=schema, matches=(_...) -> true)
265+
definition = fixture.server.events.definitions[name]
266+
descriptor = EventsMCP.event_descriptor(definition)
267+
@test descriptor["inputSchema"] == original
268+
@test descriptor["payloadSchema"] == original
269+
@test schema == original
270+
for validator in (definition.input_validator, definition.payload_validator)
271+
for value in accepted
272+
@test isvalid(validator, value)
273+
end
274+
for value in rejected
275+
@test !isvalid(validator, value)
276+
end
277+
end
278+
end
279+
end
280+
155281
@testset "Webhook lifecycle, isolation, and rotation" begin
156282
fixture = event_fixture()
157283
params = event_params(arguments=Dict("resource" => "shared", "metadata" => Dict("z" => 1.0, "a" => [true, nothing, "λ"])))

‎test/trim_compile_tests.jl‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Test
33
const _TRIM_SAFE_ERROR_BUDGET = 0
44
const _TRIM_SUPPORTED = VERSION >= v"1.12.0-rc1"
55
const _TRIM_PRE_RELEASE = !isempty(VERSION.prerelease)
6+
const _TRIM_SETUP_TIMEOUT_S = 600.0
67
const _TRIM_COMPILE_TIMEOUT_S = Sys.iswindows() ? 600.0 : 120.0
78
const _TRIM_EXECUTABLE_TIMEOUT_S = Sys.iswindows() ? 120.0 : 30.0
89
const _TRIM_USE_BUNDLE = Sys.iswindows()
@@ -13,6 +14,9 @@ const _JULIAC_ENTRYPOINT_EXPR = "using JuliaC; if isdefined(JuliaC, :main); Juli
1314
# default load path.
1415
function _clean_cmd(cmd::Cmd)
1516
env = Dict{String,String}(k => v for (k, v) in ENV if k != "JULIA_LOAD_PATH")
17+
# The executable runs on this host. Keep setup and JuliaC on the same CPU
18+
# target so JuliaC can reuse setup's precompiled package caches.
19+
env["JULIA_CPU_TARGET"] = "native"
1620
return setenv(cmd, env)
1721
end
1822

@@ -25,12 +29,14 @@ function _setup_trim_env()
2529
import Pkg
2630
Pkg.develop(path=$(repr(package_path)))
2731
Pkg.add("JuliaC")
32+
# CI disables automatic precompilation; keep cold caches out of the compile deadline.
33+
Pkg.precompile()
2834
""")
2935
println("[trim] setting up temp environment with JuliaC...")
3036
flush(stdout)
3137
exit_code, output, timed_out = _run_command_with_timeout(
3238
_clean_cmd(`$julia --startup-file=no --history-file=no --project=$env_path $setup_script`);
33-
timeout_s = 120.0,
39+
timeout_s = _TRIM_SETUP_TIMEOUT_S,
3440
log_label = "setup",
3541
)
3642
rm(setup_script; force = true)

0 commit comments

Comments
 (0)