Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/linter/rules/error-contract-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ export function lintErrorContractConformance(

const observed = new Set<JsonRpcErrorCode>();

// Direct references: `JsonRpcErrorCode.NotFound`
for (const m of cleaned.matchAll(/JsonRpcErrorCode\.(\w+)/g)) {
// Direct throws: `throw new McpError(JsonRpcErrorCode.NotFound, ...)`
for (const m of cleaned.matchAll(/\bthrow\s+new\s+McpError\s*\(\s*JsonRpcErrorCode\.(\w+)/g)) {
const value = m[1] ? CODE_NAME_TO_VALUE[m[1]] : undefined;
if (value !== undefined) observed.add(value);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/linter/error-contract-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,30 @@ describe('lintErrorContractConformance', () => {
);
expect(d).toEqual([]);
});

it('does not treat JsonRpcErrorCode comparisons as direct throws', () => {
const handler = new Function(
`return async () => {
try {
throw new Error("boom");
} catch (err) {
if (err instanceof McpError && err.code === JsonRpcErrorCode.NotFound) {
throw ctx.fail("not_found", "Not found");
}
throw err;
}
}`,
)();
const d = lintErrorContractConformance(
{
handler,
errors: [{ code: JsonRpcErrorCode.NotFound, reason: 'not_found', when: 'no match' }],
},
'tool',
'x',
);
expect(d).toEqual([]);
});
});

it('produces no diagnostics for a clean handler that uses ctx.fail', () => {
Expand Down