fix(mcp-manager): validate client fields before reading the OAuth key

Entry validation reads `oauth.credentialKey` only after the MCP client
host has validated the entry's client configuration, and refuses OAuth
on stdio before parsing the key. A settings write whose `oauth` is not
an OAuth configuration is again refused by the schema naming
`servers.<name>.oauth`, instead of a TypeError from reading its key.
This commit is contained in:
gengruilin committed 2026-11-06 03:35:14 +08:00
1 parent 8d19b4c841
commit 1ae3e12fd9
2 files changed
+12 -4

No files matched your search

+2 -4
View File
@@ -104,8 +104,6 @@ const ServerEntrySchema = z.object({
reconnect: z.any(),
}) as z<EntryInput, McpManagedServerEntry>
/** One server's child instance, probes, and any cleanup failure retained by the manager. */
interface ManagedRecord {
oauth?: McpOAuthAuthorization
@@ -621,10 +619,11 @@ export default class McpServerManager extends TypertRemoteService {
const oauthKeys = new Set<string>()
for (const [name, entry] of Object.entries(entries)) {
const path = `servers.${name}`
this.clientConfig(name, entry)
if (entry.oauth !== undefined) {
if (entry.transport !== 'streamable-http') throw new Error(path + ': OAuth requires streamable-http')
try { parseCredentialKey(entry.oauth.credentialKey) }
catch (error) { throw new Error(`${path}.oauth.credentialKey: ${(error as TypeError).message}`, { cause: error }) }
if (entry.transport !== 'streamable-http') throw new Error(path + ': OAuth requires streamable-http')
if (oauthKeys.has(entry.oauth.credentialKey)) throw new Error(path + ': OAuth credentialKey is already assigned to another server')
oauthKeys.add(entry.oauth.credentialKey)
}
@@ -655,7 +654,6 @@ export default class McpServerManager extends TypertRemoteService {
throw new Error(`${path}: "cwd" is not a streamable-http field — remove it or set transport: stdio`)
}
}
this.clientConfig(name, entry)
}
}
@@ -138,4 +138,14 @@ describe('mcp-manager real composition', () => {
expect(await readFile(profile.patchPath, 'utf8')).toBe(saved)
expect(ctx.mcpManager.list()).toEqual([])
})
it('names the oauth field when a settings write supplies an OAuth configuration without a credential key', async () => {
const ctx = await registry()
const { profile } = await managerProfile(ctx, { base: { servers: {} } })
const saved = await readFile(profile.patchPath, 'utf8')
await expect(ctx.settings.update('mcp', { servers: { one: {
transport: 'streamable-http', url: 'https://example.invalid/mcp', oauth: {}, enabled: false,
} } })).rejects.toThrow('servers.one.oauth expected')
expect(await readFile(profile.patchPath, 'utf8')).toBe(saved)
})
})