Skip to content

Commit 395b8b9

Browse files
authored
fix(mcp): return text or blob from resources/read for a resource without content (#533)
Follow-up to #529, the `resources/read` gap I mentioned there. `MCPMock.addResource(def)` allows a resource with no content (the `content` argument is optional), but `resources/read` then returns `{ "uri": ... }` with neither `text` nor `blob`. MCP resource contents must carry one of the two, so the official clients reject the whole result: ```js import { ReadResourceResultSchema } from "@modelcontextprotocol/sdk/types.js"; ReadResourceResultSchema.safeParse({ contents: [{ uri: "file:///empty" }] }).success; // false ReadResourceResultSchema.safeParse({ contents: [{ uri: "file:///empty", text: "" }] }).success; // true ``` The Python SDK (`read_resource`) fails the same way with a validation error, so a test that reads a content-less mock resource through a real client fails before it reaches anything it's testing. A smaller related gap: `resources/read` only copies `content.mimeType`, so a `mimeType` set on the resource definition (and listed by `resources/list`) is dropped when the content doesn't repeat it. **Fix** (`src/mcp-handler.ts`): - no `text` and no `blob` → return `text: ""` - `mimeType` falls back to the definition's when the content has none Resources with content are unchanged. The config-loader path already copies `mimeType` into the content, so YAML/JSON-configured resources behave as before. **Tests** (`src/__tests__/mcp-mock.test.ts`): - updated `resources/read with no content fields`, which asserted the old shape: it now expects `text: ""` and checks the result against the SDK's `ReadResourceResultSchema` - new: the definition's `mimeType` is used for a content-less resource and for content without its own `mimeType` Both fail on `main` and pass with the fix. `mcp-mock.test.ts`: 52 passed. Full suite locally: the same 35 failures as on `main` (validate-cli and mcp-fakes CLI tests, unrelated), nothing new. Typecheck, eslint and prettier are clean.
2 parents c90342c + 7dc1bf9 commit 395b8b9

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- MCP `resources/read`: a resource whose content has neither `text` nor `blob` (including one added without content) now reads as `"text": ""` (#533)
8+
- MCP `resources/read`: the read uses the resource definition's `mimeType` when its content has none (#533)
9+
510
## [1.45.1] - 2026-10-11
611

712
### Changed

‎docs/mcp-mock/index.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,12 @@ <h2>Resources</h2>
13591359
{ text: <span class="str">"# My Project\nWelcome!"</span> },
13601360
);</code></pre>
13611361
</div>
1362+
<p>
1363+
The content argument is optional. A resource whose content has neither
1364+
<code>text</code> nor <code>blob</code> (including one added without content) is read as
1365+
<code>"text": ""</code>. When the content has no <code>mimeType</code>, the read uses the
1366+
<code>mimeType</code> of the resource definition.
1367+
</p>
13621368

13631369
<h2>Prompts</h2>
13641370
<p>Register prompt templates with optional handlers:</p>

‎src/__tests__/mcp-mock.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as http from "node:http";
33
import { MCPMock } from "../mcp-mock.js";
44
import { LLMock } from "../llmock.js";
55
import type { MCPContent } from "../mcp-types.js";
6+
import { ReadResourceResultSchema } from "@modelcontextprotocol/sdk/types.js";
67

78
// ---- HTTP Helpers ----
89

@@ -782,8 +783,33 @@ describe("MCPMock", () => {
782783
});
783784
const data = JSON.parse(res.body);
784785
expect(data.result.contents[0].uri).toBe("file:///empty");
785-
expect(data.result.contents[0].text).toBeUndefined();
786+
// MCP resource contents must carry text or blob, so no content reads as empty text.
787+
expect(data.result.contents[0].text).toBe("");
786788
expect(data.result.contents[0].blob).toBeUndefined();
789+
expect(ReadResourceResultSchema.safeParse(data.result).success).toBe(true);
790+
});
791+
792+
it("resources/read falls back to the definition's mimeType", async () => {
793+
mcp = new MCPMock();
794+
mcp.addResource({ uri: "file:///notes.md", name: "Notes", mimeType: "text/markdown" });
795+
mcp.addResource(
796+
{ uri: "file:///data.csv", name: "Data", mimeType: "text/csv" },
797+
{ text: "a,b" },
798+
);
799+
const url = await mcp.start();
800+
const sessionId = await initSession(url);
801+
802+
for (const [uri, mimeType] of [
803+
["file:///notes.md", "text/markdown"],
804+
["file:///data.csv", "text/csv"],
805+
]) {
806+
const res = await jsonRpc(url, "/", "resources/read", { uri }, 1, {
807+
"mcp-session-id": sessionId,
808+
});
809+
const data = JSON.parse(res.body);
810+
expect(data.result.contents[0].mimeType).toBe(mimeType);
811+
expect(ReadResourceResultSchema.safeParse(data.result).success).toBe(true);
812+
}
787813
});
788814
});
789815

‎src/mcp-handler.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,19 @@ export function createMCPRequestHandler(state: MCPState) {
591591
if (!entry) {
592592
return jsonRpcError(id, -32602, `Unknown resource: ${uri}`);
593593
}
594+
const text = entry.content?.text;
595+
const blob = entry.content?.blob;
596+
// A resource added without content still needs text or blob: MCP clients
597+
// reject resource contents that carry neither.
598+
const mimeType = entry.content?.mimeType ?? entry.def.mimeType;
594599
return jsonRpcResult(id, {
595600
contents: [
596601
{
597602
uri,
598-
...(entry.content?.text !== undefined && { text: entry.content.text }),
599-
...(entry.content?.blob !== undefined && { blob: entry.content.blob }),
600-
...(entry.content?.mimeType !== undefined && { mimeType: entry.content.mimeType }),
603+
...(text !== undefined && { text }),
604+
...(blob !== undefined && { blob }),
605+
...(text === undefined && blob === undefined && { text: "" }),
606+
...(mimeType !== undefined && { mimeType }),
601607
},
602608
],
603609
});

0 commit comments

Comments
 (0)