Skip to content

Commit 13a615b

Browse files
authored
perf(core): convert each tool schema once (#58798)
1 parent e573115 commit 13a615b

3 files changed

Lines changed: 90 additions & 5 deletions

File tree

‎packages/core/src/tool/runtime.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,32 @@ const validateStandard = (
154154
}),
155155
)
156156

157+
// Definitions are rebuilt on every step, but schemas are immutable, so convert each one once.
158+
const inputJsonSchemas = new WeakMap<object, JsonSchema.JsonSchema>()
159+
const outputJsonSchemas = new WeakMap<object, JsonSchema.JsonSchema>()
160+
161+
const memoize = (
162+
cache: WeakMap<object, JsonSchema.JsonSchema>,
163+
schema: object,
164+
build: () => JsonSchema.JsonSchema,
165+
): JsonSchema.JsonSchema => {
166+
const cached = cache.get(schema)
167+
if (cached) return cached
168+
const built = build()
169+
cache.set(schema, built)
170+
return built
171+
}
172+
157173
const inputJsonSchema = (schema: Tool.ValueSchema<any>): JsonSchema.JsonSchema => {
158174
if (schema === undefined || schema === null) return {}
159-
if (isStandardSchema(schema)) return standardJsonSchema(schema, "input")
175+
if (isStandardSchema(schema)) return memoize(inputJsonSchemas, schema, () => standardJsonSchema(schema, "input"))
160176
if (!Schema.isSchema(schema)) return schema
161-
return emptyInputJsonSchema(schema) ?? toJsonSchema(schema)
177+
return memoize(inputJsonSchemas, schema, () => emptyInputJsonSchema(schema) ?? toJsonSchema(schema))
162178
}
163179

164180
const outputJsonSchema = (schema: Tool.ValueSchema<any>): JsonSchema.JsonSchema => {
165-
if (isStandardSchema(schema)) return standardJsonSchema(schema, "output")
166-
return Schema.isSchema(schema) ? toJsonSchema(schema) : schema
181+
if (isStandardSchema(schema)) return memoize(outputJsonSchemas, schema, () => standardJsonSchema(schema, "output"))
182+
return Schema.isSchema(schema) ? memoize(outputJsonSchemas, schema, () => toJsonSchema(schema)) : schema
167183
}
168184

169185
const standardJsonSchema = (schema: StandardSchemaV1<any, any>, io: "input" | "output"): JsonSchema.JsonSchema => {

‎packages/core/test/tool-execute.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
205205
).toBe("call_execute:x")
206206
expect(conversions).toBe(1)
207207

208+
// Converted schemas are cached by identity, so a fresh schema shows the new inventory's runtime is rebuilt.
208209
await Effect.runPromise(
209-
createCodeMode(new Map(inventory.tools)).execute({ code: 'return await tools.echo({ value: "next" })' }, context),
210+
createCodeMode(new Map([["echo", { ...echo, input: { ...echo.input } }]])).execute(
211+
{ code: 'return await tools.echo({ value: "next" })' },
212+
context,
213+
),
210214
)
211215
expect(conversions).toBe(2)
212216
})

‎packages/core/test/tool-schema.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,71 @@ test("Effect tool schemas resolve escaped definition names", () => {
193193
expect(JSON.stringify(definition(tool).inputSchema)).not.toContain("$defs")
194194
})
195195

196+
test("repeat steps reuse converted schemas instead of rebuilding them", () => {
197+
const builds: Array<string> = []
198+
const portable = {
199+
"~standard": {
200+
version: 1,
201+
vendor: "test",
202+
validate: (value: unknown) => ({ value }),
203+
jsonSchema: {
204+
input: () => {
205+
builds.push("input")
206+
return { type: "object" }
207+
},
208+
output: () => {
209+
builds.push("output")
210+
return { type: "string" }
211+
},
212+
},
213+
},
214+
}
215+
const effect: Info = {
216+
name: "effect",
217+
description: "Effect tool",
218+
input: Schema.Struct({ value: Schema.String }),
219+
output: Schema.Struct({ ok: Schema.Boolean }),
220+
execute: () => Effect.succeed({ output: { ok: true } }),
221+
}
222+
const step = () => [
223+
// Snapshots recreate some tools, such as CodeMode's, around the same schemas each step.
224+
definition({ ...effect }),
225+
definition({ ...effect, name: "portable", input: portable, output: portable }),
226+
]
227+
228+
const first = step()
229+
const before = builds.length
230+
const second = step()
231+
232+
expect(builds.length).toBe(before)
233+
second.forEach((item, index) => {
234+
expect(item.inputSchema).toBe(first[index].inputSchema)
235+
expect(item.outputSchema).toBe(first[index].outputSchema)
236+
})
237+
})
238+
239+
test("cached definitions serialize byte-identically to freshly built ones", () => {
240+
const Item = Schema.Struct({ id: Schema.String }).annotate({ identifier: "Item" })
241+
const tool: Info = {
242+
name: "pinned.tool",
243+
description: "Pinned",
244+
options: { namespace: "mcp.server" },
245+
input: Schema.Struct({
246+
offset: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
247+
items: Schema.Array(Item),
248+
}),
249+
output: Schema.Struct({}).annotate({ description: "Nothing" }),
250+
execute: () => Effect.succeed({ content: "unused" }),
251+
}
252+
// Serialized by the uncached implementation; key order matters for provider prompt caches.
253+
const expected =
254+
'{"type":"tool","name":"mcp_server_pinned_tool","description":"Pinned","inputSchema":{"type":"object","properties":{"offset":{"type":"integer","minimum":0},"items":{"type":"array","items":{"type":"object","properties":{"id":{"type":"string"}},"required":["id"],"additionalProperties":false}}},"required":["items"],"additionalProperties":false},"outputSchema":{"not":{"type":"null"},"description":"Nothing"}}'
255+
256+
expect(JSON.stringify(definition(tool))).toBe(expected)
257+
expect(JSON.stringify(definition(tool))).toBe(expected)
258+
expect(JSON.stringify(definition({ ...tool }))).toBe(expected)
259+
})
260+
196261
test.each(["Lookup Input", "café", "A/B~C% D#E"])(
197262
"Effect tool schemas inline URI-encoded definition %s",
198263
(identifier) => {

0 commit comments

Comments
 (0)