Skip to content

Commit 3f939be

Browse files
committed
readme fixes and template tests for title
1 parent 8719f93 commit 3f939be

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ server.registerResource(
7878
title: "Greeting Resource", // Display name for UI
7979
description: "Dynamic greeting generator"
8080
},
81-
async (uri, params) => ({
81+
async (uri, { name }) => ({
8282
contents: [{
8383
uri: uri.href,
84-
text: `Hello, ${params.name}!`
84+
text: `Hello, ${name}!`
8585
}]
8686
})
8787
);
@@ -143,10 +143,10 @@ server.registerResource(
143143
title: "User Profile",
144144
description: "User profile information"
145145
},
146-
async (uri, params) => ({
146+
async (uri, { userId }) => ({
147147
contents: [{
148148
uri: uri.href,
149-
text: `Profile data for user ${params.userId}`
149+
text: `Profile data for user ${userId}`
150150
}]
151151
})
152152
);
@@ -479,10 +479,10 @@ server.registerResource(
479479
title: "Echo Resource",
480480
description: "Echoes back messages as resources"
481481
},
482-
async (uri, params) => ({
482+
async (uri, { message }) => ({
483483
contents: [{
484484
uri: uri.href,
485-
text: `Resource echo: ${params.message}`
485+
text: `Resource echo: ${message}`
486486
}]
487487
})
488488
);

src/server/title.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Server } from "./index.js";
22
import { Client } from "../client/index.js";
33
import { InMemoryTransport } from "../inMemory.js";
44
import { z } from "zod";
5-
import { McpServer } from "./mcp.js";
5+
import { McpServer, ResourceTemplate } from "./mcp.js";
66

77
describe("Title field backwards compatibility", () => {
88
it("should work with tools that have title", async () => {
@@ -169,6 +169,48 @@ describe("Title field backwards compatibility", () => {
169169
expect(resources.resources[0].mimeType).toBe("text/plain");
170170
});
171171

172+
it("should work with dynamic resources using registerResource", async () => {
173+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
174+
175+
const server = new McpServer(
176+
{ name: "test-server", version: "1.0.0" },
177+
{ capabilities: {} }
178+
);
179+
180+
// Register dynamic resource with title using registerResource
181+
server.registerResource(
182+
"user-profile",
183+
new ResourceTemplate("users://{userId}/profile", { list: undefined }),
184+
{
185+
title: "User Profile",
186+
description: "User profile information"
187+
},
188+
async (uri, { userId }, extra) => ({
189+
contents: [{
190+
uri: uri.href,
191+
text: `Profile data for user ${userId}`
192+
}]
193+
})
194+
);
195+
196+
const client = new Client({ name: "test-client", version: "1.0.0" });
197+
198+
await server.server.connect(serverTransport);
199+
await client.connect(clientTransport);
200+
201+
const resourceTemplates = await client.listResourceTemplates();
202+
expect(resourceTemplates.resourceTemplates).toHaveLength(1);
203+
expect(resourceTemplates.resourceTemplates[0].name).toBe("user-profile");
204+
expect(resourceTemplates.resourceTemplates[0].title).toBe("User Profile");
205+
expect(resourceTemplates.resourceTemplates[0].description).toBe("User profile information");
206+
expect(resourceTemplates.resourceTemplates[0].uriTemplate).toBe("users://{userId}/profile");
207+
208+
// Test reading the resource
209+
const readResult = await client.readResource({ uri: "users://123/profile" });
210+
expect(readResult.contents).toHaveLength(1);
211+
expect(readResult.contents[0].text).toBe("Profile data for user 123");
212+
});
213+
172214
it("should support serverInfo with title", async () => {
173215
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
174216

0 commit comments

Comments
 (0)