|
| 1 | +import { performance } from "node:perf_hooks"; |
| 2 | + |
| 3 | +import { isCapabilityAdvertised, makeCheckResult, type CheckContext, type ObservedCheck } from "./base.js"; |
| 4 | +import type { EvidenceSummary } from "../types.js"; |
| 5 | + |
| 6 | +interface ConformanceFinding { |
| 7 | + rule: string; |
| 8 | + passed: boolean; |
| 9 | + detail: string; |
| 10 | +} |
| 11 | + |
| 12 | +function checkCapabilitiesPresent(context: CheckContext): ConformanceFinding { |
| 13 | + const caps = context.serverCapabilities; |
| 14 | + if (caps === undefined) { |
| 15 | + return { rule: "capabilities-present", passed: false, detail: "Server did not return capabilities during initialization." }; |
| 16 | + } |
| 17 | + return { rule: "capabilities-present", passed: true, detail: "Server returned capabilities object." }; |
| 18 | +} |
| 19 | + |
| 20 | +function checkServerInfo(context: CheckContext): ConformanceFinding { |
| 21 | + const caps = context.serverCapabilities; |
| 22 | + if (!caps) { |
| 23 | + return { rule: "server-info", passed: false, detail: "Cannot verify server info — no capabilities returned." }; |
| 24 | + } |
| 25 | + return { rule: "server-info", passed: true, detail: "Server provided initialization info." }; |
| 26 | +} |
| 27 | + |
| 28 | +async function checkToolsEndpoint(context: CheckContext): Promise<ConformanceFinding> { |
| 29 | + if (!isCapabilityAdvertised(context.serverCapabilities, "tools")) { |
| 30 | + return { rule: "tools-capability-match", passed: true, detail: "Tools not advertised — endpoint check skipped." }; |
| 31 | + } |
| 32 | + try { |
| 33 | + const resp = await context.client.listTools(undefined, { timeout: context.timeoutMs }); |
| 34 | + if (!Array.isArray(resp.tools)) { |
| 35 | + return { rule: "tools-capability-match", passed: false, detail: "tools/list did not return an array of tools." }; |
| 36 | + } |
| 37 | + return { rule: "tools-capability-match", passed: true, detail: `tools/list returned ${resp.tools.length} tool(s).` }; |
| 38 | + } catch (error) { |
| 39 | + const msg = error instanceof Error ? error.message : String(error); |
| 40 | + return { rule: "tools-capability-match", passed: false, detail: `Advertised tools but tools/list failed: ${msg}` }; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function checkPromptsEndpoint(context: CheckContext): Promise<ConformanceFinding> { |
| 45 | + if (!isCapabilityAdvertised(context.serverCapabilities, "prompts")) { |
| 46 | + return { rule: "prompts-capability-match", passed: true, detail: "Prompts not advertised — endpoint check skipped." }; |
| 47 | + } |
| 48 | + try { |
| 49 | + const resp = await context.client.listPrompts(undefined, { timeout: context.timeoutMs }); |
| 50 | + if (!Array.isArray(resp.prompts)) { |
| 51 | + return { rule: "prompts-capability-match", passed: false, detail: "prompts/list did not return an array of prompts." }; |
| 52 | + } |
| 53 | + return { rule: "prompts-capability-match", passed: true, detail: `prompts/list returned ${resp.prompts.length} prompt(s).` }; |
| 54 | + } catch (error) { |
| 55 | + const msg = error instanceof Error ? error.message : String(error); |
| 56 | + return { rule: "prompts-capability-match", passed: false, detail: `Advertised prompts but prompts/list failed: ${msg}` }; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +async function checkResourcesEndpoint(context: CheckContext): Promise<ConformanceFinding> { |
| 61 | + if (!isCapabilityAdvertised(context.serverCapabilities, "resources")) { |
| 62 | + return { rule: "resources-capability-match", passed: true, detail: "Resources not advertised — endpoint check skipped." }; |
| 63 | + } |
| 64 | + try { |
| 65 | + const resp = await context.client.listResources(undefined, { timeout: context.timeoutMs }); |
| 66 | + if (!Array.isArray(resp.resources)) { |
| 67 | + return { rule: "resources-capability-match", passed: false, detail: "resources/list did not return an array of resources." }; |
| 68 | + } |
| 69 | + return { rule: "resources-capability-match", passed: true, detail: `resources/list returned ${resp.resources.length} resource(s).` }; |
| 70 | + } catch (error) { |
| 71 | + const msg = error instanceof Error ? error.message : String(error); |
| 72 | + return { rule: "resources-capability-match", passed: false, detail: `Advertised resources but resources/list failed: ${msg}` }; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function checkToolResponseContent(context: CheckContext): Promise<ConformanceFinding> { |
| 77 | + if (!isCapabilityAdvertised(context.serverCapabilities, "tools")) { |
| 78 | + return { rule: "tool-response-content", passed: true, detail: "No tools — content check skipped." }; |
| 79 | + } |
| 80 | + try { |
| 81 | + const { tools } = await context.client.listTools(undefined, { timeout: context.timeoutMs }); |
| 82 | + // Find a tool safe to invoke (no required params) |
| 83 | + const safeTool = tools.find(t => { |
| 84 | + const schema = t.inputSchema as Record<string, unknown> | undefined; |
| 85 | + const required = schema?.["required"] as string[] | undefined; |
| 86 | + return !required || required.length === 0; |
| 87 | + }); |
| 88 | + if (!safeTool) { |
| 89 | + return { rule: "tool-response-content", passed: true, detail: "No safe tool to invoke — content validation skipped." }; |
| 90 | + } |
| 91 | + const result = await context.client.callTool({ name: safeTool.name, arguments: {} }, undefined, { timeout: context.timeoutMs }); |
| 92 | + if (!Array.isArray(result.content)) { |
| 93 | + return { rule: "tool-response-content", passed: false, detail: `Tool "${safeTool.name}" response.content is not an array.` }; |
| 94 | + } |
| 95 | + for (const item of result.content) { |
| 96 | + const typed = item as Record<string, unknown>; |
| 97 | + if (typeof typed["type"] !== "string") { |
| 98 | + return { rule: "tool-response-content", passed: false, detail: `Tool "${safeTool.name}" response content item missing 'type' field.` }; |
| 99 | + } |
| 100 | + } |
| 101 | + return { rule: "tool-response-content", passed: true, detail: `Tool "${safeTool.name}" response has valid content array.` }; |
| 102 | + } catch (error) { |
| 103 | + const msg = error instanceof Error ? error.message : String(error); |
| 104 | + return { rule: "tool-response-content", passed: false, detail: `Tool response content check failed: ${msg}` }; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +async function checkErrorHandling(context: CheckContext): Promise<ConformanceFinding> { |
| 109 | + try { |
| 110 | + // Try calling a method that shouldn't exist |
| 111 | + await context.client.request( |
| 112 | + { method: "observatory/nonexistent", params: {} }, |
| 113 | + { method: "object" } as never, |
| 114 | + { timeout: Math.min(context.timeoutMs, 5000) }, |
| 115 | + ); |
| 116 | + // If we get here, server didn't error — that's actually acceptable per JSON-RPC |
| 117 | + return { rule: "error-handling", passed: true, detail: "Server responded to unknown method without crashing." }; |
| 118 | + } catch (error) { |
| 119 | + // Getting an error is the expected behavior |
| 120 | + const err = error as Record<string, unknown>; |
| 121 | + const code = err["code"]; |
| 122 | + if (typeof code === "number") { |
| 123 | + return { rule: "error-handling", passed: true, detail: `Server returned proper error code ${String(code)} for unknown method.` }; |
| 124 | + } |
| 125 | + if (code !== undefined && code !== null) { |
| 126 | + return { rule: "error-handling", passed: true, detail: "Server returned an error response for unknown method." }; |
| 127 | + } |
| 128 | + // Connection-level error is ok — server closed cleanly |
| 129 | + return { rule: "error-handling", passed: true, detail: "Server handled unknown method gracefully." }; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export async function runConformanceCheck(context: CheckContext): Promise<ObservedCheck> { |
| 134 | + const startedAt = performance.now(); |
| 135 | + |
| 136 | + const asyncFindings = await Promise.all([ |
| 137 | + checkToolsEndpoint(context), |
| 138 | + checkPromptsEndpoint(context), |
| 139 | + checkResourcesEndpoint(context), |
| 140 | + checkToolResponseContent(context), |
| 141 | + checkErrorHandling(context), |
| 142 | + ]); |
| 143 | + |
| 144 | + const findings = [ |
| 145 | + checkCapabilitiesPresent(context), |
| 146 | + checkServerInfo(context), |
| 147 | + ...asyncFindings, |
| 148 | + ]; |
| 149 | + |
| 150 | + const passed = findings.filter(f => f.passed).length; |
| 151 | + const failed = findings.filter(f => !f.passed).length; |
| 152 | + const total = findings.length; |
| 153 | + |
| 154 | + let status: "pass" | "partial" | "fail"; |
| 155 | + if (failed === 0) { |
| 156 | + status = "pass"; |
| 157 | + } else if (passed > failed) { |
| 158 | + status = "partial"; |
| 159 | + } else { |
| 160 | + status = "fail"; |
| 161 | + } |
| 162 | + |
| 163 | + const message = failed === 0 |
| 164 | + ? `All ${total} conformance checks passed.` |
| 165 | + : `${passed}/${total} conformance checks passed, ${failed} failed.`; |
| 166 | + |
| 167 | + const diagnostics = findings.map(f => `[${f.passed ? "pass" : "FAIL"}] ${f.rule}: ${f.detail}`); |
| 168 | + |
| 169 | + const evidence: EvidenceSummary = { |
| 170 | + endpoint: "conformance/check", |
| 171 | + advertised: true, |
| 172 | + responded: true, |
| 173 | + minimalShapePresent: failed === 0, |
| 174 | + itemCount: total, |
| 175 | + identifiers: findings.filter(f => !f.passed).map(f => f.rule), |
| 176 | + diagnostics, |
| 177 | + }; |
| 178 | + |
| 179 | + return { |
| 180 | + result: makeCheckResult( |
| 181 | + "conformance", |
| 182 | + status, |
| 183 | + performance.now() - startedAt, |
| 184 | + message, |
| 185 | + [evidence], |
| 186 | + ), |
| 187 | + }; |
| 188 | +} |
0 commit comments