Skip to content

Commit df1dd3f

Browse files
cliffhallclaude
andcommitted
fix(filesystem): report a filesystem failure while resolving a resource as an internal error
validatePath can fail on an I/O error or a timeout from realpath for a URI that is otherwise fine. Report those as -32603, like a failed read, and keep -32602 for the confinement refusals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 956e7f9 commit df1dd3f

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎src/filesystem/__tests__/file-resources.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
fileResourceContents,
2626
registerFileResources,
2727
} from "../file-resources.js";
28+
import { FsTimeoutError } from "../lib.js";
2829
import { connect, makeTempDir, quietStderr } from "./helpers.js";
2930

3031
let allowed: string;
@@ -187,6 +188,30 @@ describe("resources/read", () => {
187188
},
188189
);
189190

191+
it("reports a failure while resolving the path as an internal error", async () => {
192+
const file = path.join(allowed, "unlucky.txt");
193+
await fs.writeFile(file, "x");
194+
const client = await connectTo([allowed]);
195+
vi.spyOn(fs, "realpath").mockRejectedValueOnce(
196+
Object.assign(new Error("i/o error"), { code: "EIO" }),
197+
);
198+
const error = await readError(client, uriOf(file));
199+
expect(error.code).toBe(-32603);
200+
expect(error.message).toContain("Failed to resolve");
201+
});
202+
203+
it("reports a path resolution timeout as an internal error", async () => {
204+
const file = path.join(allowed, "slow.txt");
205+
await fs.writeFile(file, "x");
206+
const client = await connectTo([allowed]);
207+
vi.spyOn(fs, "realpath").mockRejectedValueOnce(
208+
new FsTimeoutError("realpath", file),
209+
);
210+
const error = await readError(client, uriOf(file));
211+
expect(error.code).toBe(-32603);
212+
expect(error.message).toContain("timed out");
213+
});
214+
190215
it("reports any other read failure as an internal error", async () => {
191216
const file = path.join(allowed, "locked.txt");
192217
await fs.writeFile(file, "x");

‎src/filesystem/file-resources.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
ResourceTemplate,
2929
} from "@modelcontextprotocol/server";
3030
import type { ReadResourceResult } from "@modelcontextprotocol/server";
31-
import { validatePath } from "./lib.js";
31+
import { FsTimeoutError, validatePath } from "./lib.js";
3232

3333
/** The URI template every file resource matches (RFC 6570 reserved expansion). */
3434
export const FILE_RESOURCE_TEMPLATE = "file://{+path}";
@@ -127,6 +127,19 @@ function isMissingPathError(error: unknown): boolean {
127127
return false;
128128
}
129129

130+
/**
131+
* Whether `error` is the filesystem failing (an errno error, or a call that
132+
* timed out) rather than validatePath refusing the path, which it does with a
133+
* plain Error and no errno code.
134+
*/
135+
function isOperationalError(error: unknown): boolean {
136+
return (
137+
error instanceof FsTimeoutError ||
138+
(error instanceof Error &&
139+
(error as NodeJS.ErrnoException).code !== undefined)
140+
);
141+
}
142+
130143
export interface FileResourceContext {
131144
/** The directories reads are confined to, read at the moment of each read. */
132145
allowedDirectories: () => readonly string[];
@@ -194,6 +207,14 @@ export function registerFileResources(
194207
if (isMissingPathError(error)) {
195208
throw new ResourceNotFoundError(uri.href, (error as Error).message);
196209
}
210+
// The filesystem failed while resolving the path (an I/O error, a
211+
// timeout): the request was fine, so it is the server's error.
212+
if (isOperationalError(error)) {
213+
throw new ProtocolError(
214+
ProtocolErrorCode.InternalError,
215+
`Failed to resolve ${uri.href}: ${(error as Error).message}`,
216+
);
217+
}
197218
// Outside the allowed directories, or a symlink out of them: the
198219
// message says which, as the tools do.
199220
throw new ProtocolError(

0 commit comments

Comments
 (0)