Skip to content

Commit d0b8f6c

Browse files
committed
fix(ai-sdk): hide MCP transport/config fields from the model too
createAgentDeviceTools() only removed session and mcpOutputFormat from tool schemas. stateDir was still model-visible and reached the shared executor as client configuration, letting a tool call redirect into a different daemon state directory - defeating the "one pinned session" guarantee the factory exists to provide. includeCost and responseLevel are MCP tool-config knobs in the same category, irrelevant to this adapter. Widens the hidden-field set to session/stateDir/mcpOutputFormat/ includeCost/responseLevel, and now strips them from the runtime input inside execute() too (not just the schema), so the guarantee holds even if a caller bypasses schema validation. The schema-properties filter and the input filter now share one omitHidden() helper instead of two near- duplicate implementations. Addresses the P1 review comment on #1804.
1 parent b826de0 commit d0b8f6c

2 files changed

Lines changed: 68 additions & 12 deletions

File tree

‎src/ai-sdk/__tests__/index.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,34 @@ test('defaults to the core tool set and pins the session for every call', async
9090
]);
9191
});
9292

93+
test('MCP transport/config controls are hidden from the schema and cannot reach the executor', async () => {
94+
const { tools } = await createAgentDeviceTools({ session: 'pinned-session' });
95+
96+
const schema = rawSchemaOf(tools, 'snapshot');
97+
for (const field of ['stateDir', 'includeCost', 'responseLevel', 'mcpOutputFormat']) {
98+
assert.equal(field in schema.properties, false, `${field} must not be model-visible`);
99+
}
100+
101+
// Simulate a caller that bypasses schema validation (e.g. a non-conforming
102+
// provider) and supplies these fields directly: the pinned session must
103+
// still hold, and none of them may reach the shared executor.
104+
await executeOf(
105+
tools,
106+
'snapshot',
107+
)({
108+
interactiveOnly: true,
109+
stateDir: '/tmp/attacker-controlled-state-dir',
110+
includeCost: true,
111+
responseLevel: 'full',
112+
mcpOutputFormat: 'json',
113+
session: 'attacker-supplied-session',
114+
});
115+
116+
assert.deepEqual(executorCalls, [
117+
{ name: 'snapshot', input: { interactiveOnly: true, session: 'pinned-session' } },
118+
]);
119+
});
120+
93121
test("set: 'all' includes extended-tier commands too", async () => {
94122
const { tools } = await createAgentDeviceTools({ session: 's', set: 'all' });
95123
assert.ok('devices' in tools, 'set: all reaches every MCP-exposed command, not just core');

‎src/ai-sdk/index.ts‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,26 @@ export type AgentDeviceTools = {
4949
toolApproval?: Partial<Record<string, ToolApprovalStatus>>;
5050
};
5151

52-
// Hidden unconditionally: `session` is always pinned by this factory, and
53-
// `mcpOutputFormat` only selects between the MCP text-content renderings —
54-
// irrelevant here since `execute` below returns structuredContent directly
55-
// and never reads a tool's rendered text.
56-
const ALWAYS_HIDDEN_SCHEMA_FIELDS = ['session', 'mcpOutputFormat'] as const;
52+
// Hidden unconditionally, from both the schema the model sees and the input
53+
// `execute` forwards to the shared executor:
54+
// - `session` is always pinned by this factory — the whole point is that a
55+
// tool call can never target a session other than the one passed in.
56+
// - `stateDir` selects which daemon state directory (and therefore which
57+
// daemon/session namespace) a call resolves against. Left model-visible,
58+
// it would let a call escape the pinned session into another daemon's
59+
// state entirely, defeating that guarantee.
60+
// - `mcpOutputFormat`, `includeCost`, `responseLevel` are MCP tool-config
61+
// knobs, not command arguments; irrelevant here since `execute` below
62+
// returns structuredContent directly and never reads a tool's rendered
63+
// text, and shaping the response is this factory's decision, not the
64+
// model's.
65+
const ALWAYS_HIDDEN_FIELDS = [
66+
'session',
67+
'stateDir',
68+
'mcpOutputFormat',
69+
'includeCost',
70+
'responseLevel',
71+
] as const;
5772

5873
// The registry's hand-rolled JsonSchema type and `jsonSchema()`'s expected
5974
// JSONSchema7 (re-exported from @ai-sdk/provider, not from `ai` itself) are
@@ -89,7 +104,7 @@ export async function createAgentDeviceTools(
89104
});
90105
const executor = createCommandToolExecutor();
91106

92-
const hiddenFields = new Set<string>(ALWAYS_HIDDEN_SCHEMA_FIELDS);
107+
const hiddenFields = new Set<string>(ALWAYS_HIDDEN_FIELDS);
93108
if (platform) hiddenFields.add('platform');
94109

95110
const tools: ToolSet = {};
@@ -105,8 +120,12 @@ export async function createAgentDeviceTools(
105120
? { outputSchema: jsonSchema(definition.outputSchema as unknown as Json7Schema) }
106121
: {}),
107122
execute: async (input) => {
123+
// Strip hidden fields from the model's input too, not just the schema
124+
// it was generated from: `additionalProperties: false` should already
125+
// reject an out-of-schema field, but the pinned session/state-dir
126+
// guarantee must hold even if some caller bypasses schema validation.
108127
const filledInput = {
109-
...input,
128+
...omitHidden(input, hiddenFields),
110129
session,
111130
...(platform ? { platform } : {}),
112131
};
@@ -141,10 +160,19 @@ async function importAiSdk(): Promise<typeof import('ai')> {
141160

142161
function omitSchemaProperties(schema: JsonSchema, hidden: ReadonlySet<string>): JsonSchema {
143162
if (!schema.properties) return schema;
144-
const properties: Record<string, JsonSchema> = {};
145-
for (const [key, value] of Object.entries(schema.properties)) {
146-
if (!hidden.has(key)) properties[key] = value;
147-
}
148163
const required = schema.required?.filter((key) => !hidden.has(key));
149-
return { ...schema, properties, ...(required ? { required } : {}) };
164+
return {
165+
...schema,
166+
properties: omitHidden(schema.properties, hidden),
167+
...(required ? { required } : {}),
168+
};
169+
}
170+
171+
/** Shared by both the schema (over `.properties`) and the runtime input (over the call itself). */
172+
function omitHidden<T>(record: Record<string, T>, hidden: ReadonlySet<string>): Record<string, T> {
173+
const result: Record<string, T> = {};
174+
for (const [key, value] of Object.entries(record)) {
175+
if (!hidden.has(key)) result[key] = value;
176+
}
177+
return result;
150178
}

0 commit comments

Comments
 (0)