Repository navigation
Commit 395b8b9
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.4 files changed
Lines changed: 47 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
5 | 10 | | |
6 | 11 | | |
7 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1359 | 1359 | | |
1360 | 1360 | | |
1361 | 1361 | | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
1362 | 1368 | | |
1363 | 1369 | | |
1364 | 1370 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
782 | 783 | | |
783 | 784 | | |
784 | 785 | | |
785 | | - | |
| 786 | + | |
| 787 | + | |
786 | 788 | | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
787 | 813 | | |
788 | 814 | | |
789 | 815 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
591 | 591 | | |
592 | 592 | | |
593 | 593 | | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
594 | 599 | | |
595 | 600 | | |
596 | 601 | | |
597 | 602 | | |
598 | | - | |
599 | | - | |
600 | | - | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
601 | 607 | | |
602 | 608 | | |
603 | 609 | | |
| |||
0 commit comments