|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `agent_prompt` is advertised only where an agent can answer it. |
| 5 | + * |
| 6 | + * The long-lived server (the one the stdio transport serves) used to register |
| 7 | + * `agent_prompt` unconditionally, with an `agentName` argument described as |
| 8 | + * `e.g. "ask", "build"`. On an open-framework boot that registers no agent at |
| 9 | + * all, `prompts/list` therefore advertised a prompt nothing could serve, its |
| 10 | + * description taught two names that resolve to nothing, and `prompts/get` |
| 11 | + * answered each of them with a SUCCESSFUL prompt result whose one message is |
| 12 | + * `Error: Agent "ask" not found` — text an MCP client hands its model as |
| 13 | + * instructions. |
| 14 | + * |
| 15 | + * The skill projection in the same file already follows "declared IS served": |
| 16 | + * a skill that cannot be served is not listed, and a get on it is a JSON-RPC |
| 17 | + * -32602. These pins hold `agent_prompt` to the same rule: |
| 18 | + * |
| 19 | + * - no agent registered, and the read says so ⇒ not listed; a get is the |
| 20 | + * protocol's own unknown-prompt error, not a fabricated success; |
| 21 | + * - agents registered ⇒ listed, the argument names only registered agents, |
| 22 | + * and a miss answers exactly what it answered before (#6055 kept that text |
| 23 | + * byte-stable on purpose); |
| 24 | + * - the read could not say (degraded, or it threw) ⇒ still listed: an outage |
| 25 | + * is not absence, and the prompt then answers the honest #6055 outage text |
| 26 | + * at get time. The argument names no agent, since none is known. |
| 27 | + * |
| 28 | + * Driven over the real wire (an in-memory transport into the same `McpServer` |
| 29 | + * the stdio transport attaches to): the decision under test is what a client |
| 30 | + * can list and fetch, so the client's view is the assertion. |
| 31 | + */ |
| 32 | + |
| 33 | +import { describe, it, expect, vi } from 'vitest'; |
| 34 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 35 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 36 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 37 | + |
| 38 | +const SKILL = { |
| 39 | + name: 'case_management', |
| 40 | + label: 'Case Management', |
| 41 | + description: 'Handles support case lifecycle', |
| 42 | + instructions: 'Triage the case, then resolve it once the caller confirms.', |
| 43 | + active: true, |
| 44 | +}; |
| 45 | + |
| 46 | +const AGENT = { |
| 47 | + name: 'support_triage', |
| 48 | + label: 'Support Triage', |
| 49 | + instructions: 'You triage support cases.', |
| 50 | + active: true, |
| 51 | +}; |
| 52 | + |
| 53 | +interface ServiceShape { |
| 54 | + agents?: unknown[]; |
| 55 | + skills?: unknown[]; |
| 56 | + agentList?: () => Promise<{ items: unknown[]; degraded: boolean; errors: string[] }>; |
| 57 | +} |
| 58 | + |
| 59 | +function metadataService({ agents = [], skills = [SKILL], agentList }: ServiceShape = {}) { |
| 60 | + const byName = (items: unknown[], name: string) => |
| 61 | + (items as Array<{ name: string }>).find((i) => i.name === name) ?? undefined; |
| 62 | + return { |
| 63 | + listObjects: vi.fn(async () => []), |
| 64 | + getObject: vi.fn(async () => undefined), |
| 65 | + get: vi.fn(async (type: string, name: string) => { |
| 66 | + if (type === 'agent') return byName(agents, name); |
| 67 | + if (type === 'skill') return byName(skills, name); |
| 68 | + return undefined; |
| 69 | + }), |
| 70 | + list: vi.fn(async (type: string) => { |
| 71 | + if (type === 'agent') return agents; |
| 72 | + if (type === 'skill') return skills; |
| 73 | + return []; |
| 74 | + }), |
| 75 | + listDiagnosed: vi.fn(async (type: string) => { |
| 76 | + if (type === 'agent' && agentList) return agentList(); |
| 77 | + const items = type === 'agent' ? agents : type === 'skill' ? skills : []; |
| 78 | + return { items, degraded: false, errors: [] }; |
| 79 | + }), |
| 80 | + exists: vi.fn(async () => false), |
| 81 | + register: vi.fn(), |
| 82 | + unregister: vi.fn(), |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +function logger() { |
| 87 | + return { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }; |
| 88 | +} |
| 89 | + |
| 90 | +async function connect(service: ReturnType<typeof metadataService>, log = logger()) { |
| 91 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9', logger: log as never }); |
| 92 | + await runtime.bridgePrompts(service as never); |
| 93 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 94 | + const client = new Client({ name: 'test-client', version: '0.0.0' }); |
| 95 | + await Promise.all([runtime.server.connect(serverTransport), client.connect(clientTransport)]); |
| 96 | + return { |
| 97 | + client, |
| 98 | + log, |
| 99 | + close: async () => { |
| 100 | + await client.close(); |
| 101 | + await runtime.stop().catch(() => {}); |
| 102 | + }, |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +function agentNameDescription(prompts: Array<{ name: string; arguments?: Array<{ name: string; description?: string }> }>) { |
| 107 | + const prompt = prompts.find((p) => p.name === 'agent_prompt'); |
| 108 | + return prompt?.arguments?.find((a) => a.name === 'agentName')?.description; |
| 109 | +} |
| 110 | + |
| 111 | +describe('agent_prompt — advertised only where an agent can answer it', () => { |
| 112 | + it('a boot that registers no agent does not list agent_prompt', async () => { |
| 113 | + const session = await connect(metadataService({ agents: [] })); |
| 114 | + try { |
| 115 | + const listed = await session.client.listPrompts(); |
| 116 | + const names = listed.prompts.map((p) => p.name); |
| 117 | + // The skill is still projected, so the absence below is a decision about |
| 118 | + // this one prompt, not an empty or missing prompt surface. |
| 119 | + expect(names).toContain('case_management'); |
| 120 | + expect(names).not.toContain('agent_prompt'); |
| 121 | + } finally { |
| 122 | + await session.close(); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + it('a get of agent_prompt on that boot is the protocol error, not a successful "not found" message', async () => { |
| 127 | + const session = await connect(metadataService({ agents: [] })); |
| 128 | + try { |
| 129 | + const outcome = await session.client |
| 130 | + .getPrompt({ name: 'agent_prompt', arguments: { agentName: 'ask' } }) |
| 131 | + .then((result) => ({ result, error: undefined }), (error: { code?: number }) => ({ result: undefined, error })); |
| 132 | + expect(outcome.result).toBeUndefined(); |
| 133 | + expect(outcome.error?.code).toBe(-32602); |
| 134 | + } finally { |
| 135 | + await session.close(); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it('says once at boot why the prompt is not advertised', async () => { |
| 140 | + const session = await connect(metadataService({ agents: [] })); |
| 141 | + try { |
| 142 | + const lines = session.log.info.mock.calls.map((c) => String(c[0])); |
| 143 | + expect(lines.some((l) => l.includes('agent_prompt') && l.includes('no agent'))).toBe(true); |
| 144 | + expect(lines).not.toContain('[MCP] Agent prompts bridged'); |
| 145 | + } finally { |
| 146 | + await session.close(); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + it('a boot with agents lists it, and the argument names only agents the boot holds', async () => { |
| 151 | + const session = await connect(metadataService({ agents: [AGENT] })); |
| 152 | + try { |
| 153 | + const listed = await session.client.listPrompts(); |
| 154 | + expect(listed.prompts.map((p) => p.name)).toContain('agent_prompt'); |
| 155 | + const description = agentNameDescription(listed.prompts); |
| 156 | + expect(description).toContain('"support_triage"'); |
| 157 | + expect(description).not.toContain('"ask"'); |
| 158 | + expect(description).not.toContain('"build"'); |
| 159 | + expect(session.log.info).toHaveBeenCalledWith('[MCP] Agent prompts bridged'); |
| 160 | + } finally { |
| 161 | + await session.close(); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + it('on that boot a registered agent is served and a miss answers exactly as before (#6055)', async () => { |
| 166 | + const session = await connect(metadataService({ agents: [AGENT] })); |
| 167 | + try { |
| 168 | + const served = await session.client.getPrompt({ name: 'agent_prompt', arguments: { agentName: 'support_triage' } }); |
| 169 | + expect(served.messages[0].content).toEqual({ type: 'text', text: 'You triage support cases.' }); |
| 170 | + |
| 171 | + const miss = await session.client.getPrompt({ name: 'agent_prompt', arguments: { agentName: 'nope' } }); |
| 172 | + expect(miss.messages).toEqual([ |
| 173 | + { role: 'user', content: { type: 'text', text: 'Error: Agent "nope" not found' } }, |
| 174 | + ]); |
| 175 | + } finally { |
| 176 | + await session.close(); |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + it('an agent read that reports itself degraded keeps the prompt listed, naming no agent', async () => { |
| 181 | + const session = await connect( |
| 182 | + metadataService({ |
| 183 | + agentList: async () => ({ items: [], degraded: true, errors: ['loader sys_metadata: connection refused'] }), |
| 184 | + }), |
| 185 | + ); |
| 186 | + try { |
| 187 | + const listed = await session.client.listPrompts(); |
| 188 | + expect(listed.prompts.map((p) => p.name)).toContain('agent_prompt'); |
| 189 | + const description = agentNameDescription(listed.prompts) ?? ''; |
| 190 | + expect(description).not.toContain('"ask"'); |
| 191 | + expect(description).not.toContain('e.g.'); |
| 192 | + expect(session.log.warn).toHaveBeenCalledWith( |
| 193 | + expect.stringContaining('agent_prompt'), |
| 194 | + expect.objectContaining({ errors: ['loader sys_metadata: connection refused'] }), |
| 195 | + ); |
| 196 | + } finally { |
| 197 | + await session.close(); |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + it('an agent read that throws keeps the prompt listed', async () => { |
| 202 | + const session = await connect( |
| 203 | + metadataService({ |
| 204 | + agentList: async () => { |
| 205 | + throw new Error('unknown metadata type'); |
| 206 | + }, |
| 207 | + }), |
| 208 | + ); |
| 209 | + try { |
| 210 | + const listed = await session.client.listPrompts(); |
| 211 | + expect(listed.prompts.map((p) => p.name)).toContain('agent_prompt'); |
| 212 | + expect(session.log.warn).toHaveBeenCalledWith( |
| 213 | + expect.stringContaining('agent_prompt'), |
| 214 | + expect.objectContaining({ errors: ['unknown metadata type'] }), |
| 215 | + ); |
| 216 | + } finally { |
| 217 | + await session.close(); |
| 218 | + } |
| 219 | + }); |
| 220 | + |
| 221 | + it('with no agent and no skill the prompts capability is not advertised at all (#8034)', async () => { |
| 222 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' }); |
| 223 | + await runtime.bridgePrompts(metadataService({ agents: [], skills: [] }) as never); |
| 224 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 225 | + const client = new Client({ name: 'test-client', version: '0.0.0' }); |
| 226 | + await Promise.all([runtime.server.connect(serverTransport), client.connect(clientTransport)]); |
| 227 | + try { |
| 228 | + expect(client.getServerCapabilities()?.prompts).toBeUndefined(); |
| 229 | + } finally { |
| 230 | + await client.close(); |
| 231 | + await runtime.stop().catch(() => {}); |
| 232 | + } |
| 233 | + }); |
| 234 | +}); |
0 commit comments