Skip to content

Commit 96640ea

Browse files
antfuboterkamyaman
authored andcommitted
refactor(mcp): trim resource template listing and redundant tests
Undefined fields drop out of the JSON result, so the listings pass them as is. The devframe and hub initiate tests that only repeated what the HTTP and in-memory tests already catch are removed.
1 parent 56f387d commit 96640ea

3 files changed

Lines changed: 5 additions & 79 deletions

File tree

‎packages/agentic/src/mcp/build-server.ts‎

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,10 @@ function registerResourceHandlers(
361361
}
362362
}
363363

364-
resources.push(...await listTemplateResources(ctx, templates))
364+
for (const { template } of templates) {
365+
for (const entry of await template.list?.(ctx) ?? [])
366+
resources.push({ ...entry, mimeType: entry.mimeType ?? template.mimeType })
367+
}
365368

366369
return { resources }
367370
})
@@ -435,34 +438,10 @@ interface ResourceListing {
435438
mimeType?: string
436439
}
437440

438-
/** The entries the `list` of each template returns, in template order, for `resources/list`. */
439-
async function listTemplateResources(ctx: DevframeNodeContext, templates: CompiledResourceTemplate[]): Promise<ResourceListing[]> {
440-
const resources: ResourceListing[] = []
441-
for (const { template } of templates) {
442-
for (const entry of await template.list?.(ctx) ?? []) {
443-
const mimeType = entry.mimeType ?? template.mimeType
444-
resources.push({
445-
uri: entry.uri,
446-
name: entry.name,
447-
...(entry.title !== undefined ? { title: entry.title } : {}),
448-
...(entry.description !== undefined ? { description: entry.description } : {}),
449-
...(mimeType !== undefined ? { mimeType } : {}),
450-
})
451-
}
452-
}
453-
return resources
454-
}
455-
456441
/** Serve `resources/templates/list`: one entry per template, in the given order. */
457442
function registerResourceTemplateHandlers(server: Server, templates: CompiledResourceTemplate[]): void {
458443
server.setRequestHandler('resources/templates/list', async () => ({
459-
resourceTemplates: templates.map(({ template }) => ({
460-
uriTemplate: template.uriTemplate,
461-
name: template.name,
462-
...(template.title !== undefined ? { title: template.title } : {}),
463-
...(template.description !== undefined ? { description: template.description } : {}),
464-
...(template.mimeType !== undefined ? { mimeType: template.mimeType } : {}),
465-
})),
444+
resourceTemplates: templates.map(({ template: { uriTemplate, name, title, description, mimeType } }) => ({ uriTemplate, name, title, description, mimeType })),
466445
}))
467446
}
468447

‎packages/devframe/src/adapters/__tests__/initiate.test.ts‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -393,46 +393,6 @@ describe('adapters/handler', () => {
393393
}
394394
})
395395

396-
it('mcp: resource templates reach the route through resources/templates/list and resources/read', async () => {
397-
const wsPort = await getPort({ port: 18148, host: '127.0.0.1' })
398-
const unsubscribe = vi.fn()
399-
const devtools = initDevframe(defineTestDef('handler-mcp-templates'), {
400-
base: '/__handler-mcp-templates/',
401-
auth: false,
402-
mcp: {
403-
resourceTemplates: [{
404-
uriTemplate: 'handler://page/{slug}',
405-
name: 'page',
406-
mimeType: 'text/plain',
407-
read: params => ({ text: `Page ${params.slug}.` }),
408-
}],
409-
onResourcesChanged: () => unsubscribe,
410-
},
411-
ws: { port: wsPort },
412-
})
413-
414-
const call = (method: string, params?: Record<string, unknown>) => devtools.handler(new Request('http://localhost:3000/__handler-mcp-templates/__mcp', {
415-
method: 'POST',
416-
headers: {
417-
'content-type': 'application/json',
418-
'accept': 'application/json, text/event-stream',
419-
'origin': 'http://localhost:3000',
420-
},
421-
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
422-
})).then(res => res.text())
423-
424-
try {
425-
await devtools.ready
426-
expect(await call('resources/templates/list')).toContain('"uriTemplate":"handler://page/{slug}"')
427-
expect(await call('resources/read', { uri: 'handler://page/home' })).toContain('Page home.')
428-
expect(await call('resources/read', { uri: 'handler://page/a/b' })).toContain('unknown resource URI')
429-
}
430-
finally {
431-
await devtools.close()
432-
}
433-
expect(unsubscribe).toHaveBeenCalledTimes(1)
434-
})
435-
436396
// The `'auto'` default: an omitted `mcp` mounts the route exactly when
437397
// `setup()` left a non-empty agent surface.
438398
function defineAgentTestDef(id: string) {

‎packages/hub/src/node/__tests__/initiate.test.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -482,19 +482,6 @@ describe('initHub', () => {
482482
})
483483

484484
describe('aggregate MCP resource templates', () => {
485-
it('answers resources/templates/list with method not found by default', async () => {
486-
const wsPort = await getPort({ port: 18241, host: '127.0.0.1' })
487-
const hub = initHub({ base: DEVFRAMES_HUB_BASE, auth: false, host: '127.0.0.1', ws: { port: wsPort }, mcp: true, devframes: [makeFrame('alpha')] })
488-
489-
try {
490-
await hub.ready
491-
expect(await callMcp(hub, 'resources/templates/list')).toContain('-32601')
492-
}
493-
finally {
494-
await hub.close()
495-
}
496-
})
497-
498485
it('serves mcp.resourceTemplates and wires mcp.onResourcesChanged to the hub context', async () => {
499486
const wsPort = await getPort({ port: 18242, host: '127.0.0.1' })
500487
const unsubscribe = vi.fn()

0 commit comments

Comments
 (0)