GeezerVison is showing github.com — feat(server): cut T3's MCP tool schemas by a third and merge 18 tools… · pingdotgg/t3code@ed44f25
File treeExpand file treeCollapse file tree Open diff view settings client-runtime/src/work-log
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/threadActivity.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -643,7 +643,10 @@ function itemToolPresentation(item: OrchestrationV2TurnItem): T3McpToolPresentat | 643 | 643 | if (item.type !== "dynamic_tool") { | 644 | 644 | return null; | 645 | 645 | } | 646 | | - return resolveT3McpToolPresentation(item.toolName) ?? resolveT3McpToolPresentation(item.title); | | 646 | + return ( | | 647 | + resolveT3McpToolPresentation(item.toolName, item.input) ?? | | 648 | + resolveT3McpToolPresentation(item.title, item.input) | | 649 | + ); | 647 | 650 | } | 648 | 651 |
| 649 | 652 | function itemSummary( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/McpHttpServer.test.ts +24-1Lines changed: 24 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -344,7 +344,7 @@ it.effect("refuses preview tools to a client outside a thread before they run", | 344 | 344 | client: { sessionId: "session-1", label: "Claude Code", access: "read-only" as const }, | 345 | 345 | }; | 346 | 346 | const click = yield* server | 347 | | - .callTool({ name: "preview_click", arguments: { locator: "text=Send" } }) | | 347 | + .callTool({ name: "preview_act", arguments: { action: "click", locator: "text=Send" } }) | 348 | 348 | .pipe( | 349 | 349 | Effect.provideService(McpInvocationContext.McpInvocationContext, readOnly), | 350 | 350 | Effect.provideService(McpSchema.McpServerClient, client), | @@ -356,6 +356,29 @@ it.effect("refuses preview tools to a client outside a thread before they run", | 356 | 356 | }).pipe(Effect.provide(layerTest)), | 357 | 357 | ); | 358 | 358 |
| | 359 | +it.effect("checks each preview_act action against that action's own input", () => | | 360 | + Effect.scoped( | | 361 | + Effect.gen(function* () { | | 362 | + const inputs = yield* serveSnapshots("mcp-act-client", {}); | | 363 | + | | 364 | + const missingKey = yield* callTool("preview_act", { action: "press" }); | | 365 | + expect(missingKey.isError).toBe(true); | | 366 | + expect(inputs).toEqual([]); | | 367 | + | | 368 | + const click = yield* callTool("preview_act", { | | 369 | + action: "click", | | 370 | + locator: "text=Send", | | 371 | + key: "Enter", | | 372 | + }); | | 373 | + expect(click.isError).toBe(false); | | 374 | + expect(inputs[0]).toEqual({ locator: "text=Send" }); | | 375 | + | | 376 | + yield* callTool("preview_act", { action: "type", selector: "#name", text: "Ada" }); | | 377 | + expect(inputs.at(-2)).toEqual({ selector: "#name", text: "Ada" }); | | 378 | + }), | | 379 | + ).pipe(Effect.provide(layerTest)), | | 380 | +); | | 381 | + | 359 | 382 | it.effect("decodes null optional arguments the way it decodes omitted ones", () => { | 360 | 383 | const calls: Array<unknown> = []; | 361 | 384 | return Effect.gen(function* () { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/McpHttpServer.ts +29-15Lines changed: 29 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ import { | 35 | 35 | MAX_REPORTED_DEPTH, | 36 | 36 | toolOutcome, | 37 | 37 | } from "../telemetry/ProviderDimensions.ts"; | | 38 | +import { advertisedJsonSchema } from "./advertisedJsonSchema.ts"; | 38 | 39 | import * as McpInvocationContext from "./McpInvocationContext.ts"; | 39 | 40 | import * as McpToolAccess from "./McpToolAccess.ts"; | 40 | 41 | import * as OrchestratorMcpService from "./OrchestratorMcpService.ts"; | @@ -450,7 +451,7 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot | 450 | 451 | tool: new McpSchema.Tool({ | 451 | 452 | name: tool.name, | 452 | 453 | description: Tool.getDescription(tool), | 453 | | - inputSchema: Tool.getJsonSchema(tool), | | 454 | + inputSchema: advertisedJsonSchema(tool), | 454 | 455 | annotations: { | 455 | 456 | ...Context.getOption(tool.annotations, Tool.Title).pipe( | 456 | 457 | Option.map((title) => ({ title })), | @@ -634,7 +635,7 @@ const registerImageTool = Effect.fn("McpHttpServer.registerImageTool")(function* | 634 | 635 | tool: new McpSchema.Tool({ | 635 | 636 | name: tool.name, | 636 | 637 | description: Tool.getDescription(tool), | 637 | | - inputSchema: Tool.getJsonSchema(tool), | | 638 | + inputSchema: advertisedJsonSchema(tool), | 638 | 639 | annotations: { | 639 | 640 | ...Context.getOption(tool.annotations, Tool.Title).pipe( | 640 | 641 | Option.map((title) => ({ title })), | @@ -751,21 +752,31 @@ const HANDOFF_TOOLS: ReadonlySet<string> = new Set([ | 751 | 752 | "t3_thread_launch", | 752 | 753 | "t3_thread_send", | 753 | 754 | "t3_thread_send_attachments", | 754 | | - "t3_thread_fork", | 755 | | - "t3_thread_merge_back", | 756 | | - "t3_queue_edit", | 757 | | - "t3_queue_promote_to_steer", | 758 | | - "t3_pending_request_respond", | 759 | 755 | "schedule_task", | 760 | 756 | "run_scheduled_task_now", | 761 | 757 | ]); | 762 | 758 |
| 763 | | -/** Handoff tools whose result is only `{ sequence }`; their input names the receiving thread. */ | 764 | | -const INPUT_TARGET_TOOLS: ReadonlySet<string> = new Set([ | 765 | | - "t3_queue_edit", | 766 | | - "t3_queue_promote_to_steer", | 767 | | - "t3_pending_request_respond", | 768 | | -]); | | 759 | +/** Tool actions that hand work over with only a `{ sequence }` result; the input names the thread. */ | | 760 | +const INPUT_TARGET_ACTIONS: Readonly<Record<string, ReadonlySet<string>>> = { | | 761 | + t3_queue: new Set(["edit", "promote_to_steer"]), | | 762 | + t3_pending_request: new Set(["respond"]), | | 763 | +}; | | 764 | + | | 765 | +/** Tool actions that hand work over and name the receiving thread in their result. */ | | 766 | +const RESULT_TARGET_ACTIONS: Readonly<Record<string, ReadonlySet<string>>> = { | | 767 | + t3_thread_transfer: new Set(["fork", "merge_back"]), | | 768 | +}; | | 769 | + | | 770 | +const hasAction = ( | | 771 | + actions: Readonly<Record<string, ReadonlySet<string>>>, | | 772 | + tool: string, | | 773 | + args: unknown, | | 774 | +) => | | 775 | + actions[tool]?.has(String((args as { readonly action?: unknown } | null | undefined)?.action)) === | | 776 | + true; | | 777 | + | | 778 | +const isInputTargetHandoff = (tool: string, args: unknown) => | | 779 | + hasAction(INPUT_TARGET_ACTIONS, tool, args); | 769 | 780 |
| 770 | 781 | const resultThreadKeys = ["childThreadId", "targetThreadId", "boundThreadId", "threadId"] as const; | 771 | 782 |
| @@ -787,7 +798,7 @@ const handoffTargetIds = ( | 787 | 798 | args: unknown, | 788 | 799 | result: McpSchema.CallToolResult | undefined, | 789 | 800 | ): ReadonlyArray<string> => { | 790 | | - if (!INPUT_TARGET_TOOLS.has(tool)) return resultThreadIds(result?.structuredContent); | | 801 | + if (!isInputTargetHandoff(tool, args)) return resultThreadIds(result?.structuredContent); | 791 | 802 | if (result === undefined || toolOutcome(result).outcome !== "ok") return []; | 792 | 803 | const threadId = (args as { readonly threadId?: unknown } | null | undefined)?.threadId; | 793 | 804 | return typeof threadId === "string" ? [threadId] : []; | @@ -853,7 +864,10 @@ const withToolAnalytics = | 853 | 864 | result: McpSchema.CallToolResult | undefined, | 854 | 865 | durationMs: number, | 855 | 866 | ) { | 856 | | - const handoff = HANDOFF_TOOLS.has(tool); | | 867 | + const handoff = | | 868 | + HANDOFF_TOOLS.has(tool) || | | 869 | + isInputTargetHandoff(tool, args) || | | 870 | + hasAction(RESULT_TARGET_ACTIONS, tool, args); | 857 | 871 | const outcome = toolOutcome(result); | 858 | 872 | // Agents call status, read, and preview tools in tight loops, so an | 859 | 873 | // event per successful call floods analytics. Only failures count there. | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/McpToolAccess.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -181,6 +181,16 @@ export const startsThreads = <P, A, E, R>( | 181 | 181 | }), | 182 | 182 | ); | 183 | 183 |
| | 184 | +/** One tool with two access rules, picked per call, such as create versus update. */ | | 185 | +export const either = <P, A, E, R, A2, E2, R2>( | | 186 | + useFirst: (params: P) => boolean, | | 187 | + first: Declaration<(params: P) => Effect.Effect<A, E, R>>, | | 188 | + second: Declaration<(params: P) => Effect.Effect<A2, E2, R2>>, | | 189 | +) => | | 190 | + declare((params: P): Effect.Effect<A | A2, E | E2, R | R2> => | | 191 | + useFirst(params) ? checkedHandler(first)(params) : checkedHandler(second)(params), | | 192 | + ); | | 193 | + | 184 | 194 | const fullAccessRequired = | 185 | 195 | "Changing projects or environment settings needs a live full-access/default calling thread or a full-access client."; | 186 | 196 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/ThreadMetadataMcpService.ts -30Lines changed: 0 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,14 +36,6 @@ function errorMessage(error: unknown): string { | 36 | 36 | return error instanceof Error ? error.message : String(error); | 37 | 37 | } | 38 | 38 |
| 39 | | -function threadLookupFailure( | 40 | | - error: ThreadManagementService.ThreadManagementError, | 41 | | -): OrchestratorMcpFailure { | 42 | | - return error._tag === "ThreadManagementThreadNotFoundError" | 43 | | - ? failure("thread_not_found", error.message) | 44 | | - : failure("orchestration_error", error.message); | 45 | | -} | 46 | | - | 47 | 39 | function stablePart(value: string): string { | 48 | 40 | return encodeURIComponent(value); | 49 | 41 | } | @@ -70,7 +62,6 @@ function commandId(input: { | 70 | 62 | function metadataCommand(input: { | 71 | 63 | readonly commandId: CommandId; | 72 | 64 | readonly threadId: ThreadId; | 73 | | - readonly projectId: OrchestrationV2AppThread["projectId"]; | 74 | 65 | readonly update: ThreadMetadataMcpUpdateInput; | 75 | 66 | }): Extract<OrchestrationV2Command, { readonly type: "thread.metadata.update" }> { | 76 | 67 | switch (input.update.action) { | @@ -88,23 +79,6 @@ function metadataCommand(input: { | 88 | 79 | threadId: input.threadId, | 89 | 80 | regenerateTitle: true, | 90 | 81 | }; | 91 | | - case "link_pull_request": | 92 | | - return { | 93 | | - type: "thread.metadata.update", | 94 | | - commandId: input.commandId, | 95 | | - threadId: input.threadId, | 96 | | - linkedPullRequest: { | 97 | | - projectId: input.projectId, | 98 | | - ...input.update.pullRequest!, | 99 | | - }, | 100 | | - }; | 101 | | - case "unlink_pull_request": | 102 | | - return { | 103 | | - type: "thread.metadata.update", | 104 | | - commandId: input.commandId, | 105 | | - threadId: input.threadId, | 106 | | - linkedPullRequest: null, | 107 | | - }; | 108 | 82 | } | 109 | 83 | } | 110 | 84 |
| @@ -170,9 +144,6 @@ const make = Effect.gen(function* () { | 170 | 144 | if (shell === null || shell.deletedAt !== null) { | 171 | 145 | return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`); | 172 | 146 | } | 173 | | - const target = yield* threadManagement | 174 | | - .getProjectThreadRecords({ projectId: shell.projectId, threadId }, []) | 175 | | - .pipe(Effect.mapError(threadLookupFailure)); | 176 | 147 | const requestKey = | 177 | 148 | input.clientRequestId === undefined | 178 | 149 | ? yield* crypto.randomUUIDv4().pipe(Effect.orDie) | @@ -188,7 +159,6 @@ const make = Effect.gen(function* () { | 188 | 159 | metadataCommand({ | 189 | 160 | commandId: updateCommandId, | 190 | 161 | threadId, | 191 | | - projectId: target.thread.projectId, | 192 | 162 | update: input, | 193 | 163 | }), | 194 | 164 | ) | |
Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { expect, it } from "@effect/vitest"; | | 2 | +import * as Schema from "effect/Schema"; | | 3 | +import { Tool } from "effect/ai"; | | 4 | + | | 5 | +import { advertisedJsonSchema } from "./advertisedJsonSchema.ts"; | | 6 | + | | 7 | +it("drops JSON noise, merges tagged unions, and keeps null where it is required", () => { | | 8 | + const Every = Schema.Struct({ ms: Schema.Int }); | | 9 | + const tool = Tool.make("example", { | | 10 | + parameters: Schema.Struct({ | | 11 | + threadId: Schema.optional(Schema.NullOr(Schema.String)), | | 12 | + beforeRunId: Schema.NullOr(Schema.String), | | 13 | + limit: Schema.optional(Schema.Int), | | 14 | + every: Schema.Union([Every, Schema.fromJsonString(Every)]), | | 15 | + target: Schema.optional( | | 16 | + Schema.NullOr( | | 17 | + Schema.Union([ | | 18 | + Schema.Struct({ kind: Schema.Literal("url"), url: Schema.String }), | | 19 | + Schema.Struct({ kind: Schema.Literal("number"), number: Schema.Int }), | | 20 | + ]), | | 21 | + ), | | 22 | + ), | | 23 | + }), | | 24 | + }); | | 25 | + | | 26 | + expect(advertisedJsonSchema(tool)).toEqual({ | | 27 | + type: "object", | | 28 | + properties: { | | 29 | + threadId: { type: "string" }, | | 30 | + beforeRunId: { anyOf: [{ type: "string" }, { type: "null" }] }, | | 31 | + limit: { type: "integer" }, | | 32 | + every: { type: "object", properties: { ms: { type: "integer" } }, required: ["ms"] }, | | 33 | + target: { | | 34 | + type: "object", | | 35 | + properties: { | | 36 | + kind: { type: "string", enum: ["url", "number"] }, | | 37 | + url: { type: "string" }, | | 38 | + number: { type: "integer" }, | | 39 | + }, | | 40 | + required: ["kind"], | | 41 | + }, | | 42 | + }, | | 43 | + required: ["beforeRunId", "every"], | | 44 | + }); | | 45 | +}); |
Collapse file Copy file name to clipboard +106Lines changed: 106 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Predicate from "effect/Predicate"; | | 2 | +import { Tool } from "effect/ai"; | | 3 | + | | 4 | +type Json = unknown; | | 5 | +type JsonObject = { [key: string]: Json }; | | 6 | + | | 7 | +const isNullSchema = (value: Json) => | | 8 | + Predicate.isObject(value) && value.type === "null" && Object.keys(value).length === 1; | | 9 | + | | 10 | +const isBareAnyOf = (value: Json): value is { anyOf: Array<Json> } => | | 11 | + Predicate.isObject(value) && Array.isArray(value.anyOf) && Object.keys(value).length === 1; | | 12 | + | | 13 | +// A `Schema.fromJsonString` fallback. Advertised beside object shapes, it invites stringified input. | | 14 | +const isJsonStringSchema = (value: Json) => | | 15 | + Predicate.isObject(value) && | | 16 | + value.type === "string" && | | 17 | + value.contentMediaType === "application/json"; | | 18 | + | | 19 | +const SAFE_INTEGER_BOUNDS: Record<string, number> = { | | 20 | + maximum: Number.MAX_SAFE_INTEGER, | | 21 | + minimum: Number.MIN_SAFE_INTEGER, | | 22 | +}; | | 23 | + | | 24 | +const tagOf = (branch: JsonObject, key: string): Json => { | | 25 | + const property = Predicate.isObject(branch.properties) ? branch.properties[key] : undefined; | | 26 | + return Predicate.isObject(property) && Array.isArray(property.enum) && property.enum.length === 1 | | 27 | + ? property.enum[0] | | 28 | + : undefined; | | 29 | +}; | | 30 | + | | 31 | +/** One object for a tagged union of objects: each property once, the tag listing every value. */ | | 32 | +const mergeTaggedObjects = (branches: ReadonlyArray<Json>): JsonObject | undefined => { | | 33 | + if (branches.length < 2) return undefined; | | 34 | + const objects = branches.filter( | | 35 | + (branch): branch is JsonObject & { properties: JsonObject } => | | 36 | + Predicate.isObject(branch) && | | 37 | + branch.type === "object" && | | 38 | + Predicate.isObject(branch.properties), | | 39 | + ); | | 40 | + if (objects.length !== branches.length) return undefined; | | 41 | + const tag = Object.keys(objects[0]!.properties).find((key) => { | | 42 | + const values = objects.map((object) => tagOf(object, key)); | | 43 | + return values.every((value) => value !== undefined) && new Set(values).size === values.length; | | 44 | + }); | | 45 | + if (tag === undefined) return undefined; | | 46 | + const properties: JsonObject = { | | 47 | + [tag]: { type: "string", enum: objects.map((object) => tagOf(object, tag)) }, | | 48 | + }; | | 49 | + for (const object of objects) { | | 50 | + for (const [name, property] of Object.entries(object.properties)) { | | 51 | + if (name === tag) continue; | | 52 | + const seen = properties[name]; | | 53 | + if (seen === undefined) properties[name] = property; | | 54 | + else if (JSON.stringify(seen) !== JSON.stringify(property)) { | | 55 | + const options = Predicate.isObject(seen) && isBareAnyOf(seen) ? seen.anyOf : [seen]; | | 56 | + if (!options.some((option) => JSON.stringify(option) === JSON.stringify(property))) | | 57 | + properties[name] = { anyOf: [...options, property] }; | | 58 | + } | | 59 | + } | | 60 | + } | | 61 | + const [first = [], ...others] = objects.map((object) => | | 62 | + Array.isArray(object.required) ? object.required : [], | | 63 | + ); | | 64 | + const requiredByAll = first.filter( | | 65 | + (name) => name !== tag && others.every((required) => required.includes(name)), | | 66 | + ); | | 67 | + return { type: "object", properties, required: [tag, ...requiredByAll] }; | | 68 | +}; | | 69 | + | | 70 | +const compact = (schema: Json, optional: boolean): Json => { | | 71 | + if (Array.isArray(schema)) return schema.map((item) => compact(item, false)); | | 72 | + if (!Predicate.isObject(schema)) return schema; | | 73 | + const required = new Set(Array.isArray(schema.required) ? schema.required : []); | | 74 | + let result: JsonObject = {}; | | 75 | + for (const [key, value] of Object.entries(schema)) { | | 76 | + if (key === "additionalProperties" && value === true) continue; | | 77 | + if (SAFE_INTEGER_BOUNDS[key] === value) continue; | | 78 | + result[key] = | | 79 | + key === "properties" && Predicate.isObject(value) | | 80 | + ? Object.fromEntries( | | 81 | + Object.entries(value).map(([name, property]) => [ | | 82 | + name, | | 83 | + compact(property, !required.has(name)), | | 84 | + ]), | | 85 | + ) | | 86 | + : compact(value, false); | | 87 | + } | | 88 | + if (Array.isArray(result.anyOf)) { | | 89 | + const branches = result.anyOf | | 90 | + .flatMap((branch) => (isBareAnyOf(branch) ? branch.anyOf : [branch])) | | 91 | + .filter((branch) => !(optional && isNullSchema(branch)) && !isJsonStringSchema(branch)); | | 92 | + const { anyOf: _anyOf, ...rest } = result; | | 93 | + const only = branches.length === 1 ? branches[0] : mergeTaggedObjects(branches); | | 94 | + result = Predicate.isObject(only) ? { ...only, ...rest } : { ...rest, anyOf: branches }; | | 95 | + } | | 96 | + return result; | | 97 | +}; | | 98 | + | | 99 | +/** | | 100 | + * The input schema T3 advertises for a tool. Drops JSON noise that every thread pays for: | | 101 | + * `additionalProperties: true`, safe-integer bounds, nested `anyOf`, JSON-string fallbacks, and | | 102 | + * the `null` branch of optional properties. A tagged union of objects becomes one object. | | 103 | + * Decoding still uses the full schema, so each tag's own required fields still apply. | | 104 | + */ | | 105 | +export const advertisedJsonSchema = (tool: Tool.Any) => | | 106 | + compact(Tool.getJsonSchema(tool), false) as ReturnType<typeof Tool.getJsonSchema>; |
|