readme fixes and template tests for title

This commit is contained in:
ihrpr 2025-06-16 11:21:07 +01:00
commit 3f939be6c7
No known key found for this signature in database
2 changed files with 49 additions and 7 deletions

View file

@ -78,10 +78,10 @@ server.registerResource(
title: "Greeting Resource", // Display name for UI
description: "Dynamic greeting generator"
},
async (uri, params) => ({
async (uri, { name }) => ({
contents: [{
uri: uri.href,
text: `Hello, ${params.name}!`
text: `Hello, ${name}!`
}]
})
);
@ -143,10 +143,10 @@ server.registerResource(
title: "User Profile",
description: "User profile information"
},
async (uri, params) => ({
async (uri, { userId }) => ({
contents: [{
uri: uri.href,
text: `Profile data for user ${params.userId}`
text: `Profile data for user ${userId}`
}]
})
);
@ -479,10 +479,10 @@ server.registerResource(
title: "Echo Resource",
description: "Echoes back messages as resources"
},
async (uri, params) => ({
async (uri, { message }) => ({
contents: [{
uri: uri.href,
text: `Resource echo: ${params.message}`
text: `Resource echo: ${message}`
}]
})
);

View file

@ -2,7 +2,7 @@ import { Server } from "./index.js";
import { Client } from "../client/index.js";
import { InMemoryTransport } from "../inMemory.js";
import { z } from "zod";
import { McpServer } from "./mcp.js";
import { McpServer, ResourceTemplate } from "./mcp.js";
describe("Title field backwards compatibility", () => {
it("should work with tools that have title", async () => {
@ -169,6 +169,48 @@ describe("Title field backwards compatibility", () => {
expect(resources.resources[0].mimeType).toBe("text/plain");
});
it("should work with dynamic resources using registerResource", async () => {
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const server = new McpServer(
{ name: "test-server", version: "1.0.0" },
{ capabilities: {} }
);
// Register dynamic resource with title using registerResource
server.registerResource(
"user-profile",
new ResourceTemplate("users://{userId}/profile", { list: undefined }),
{
title: "User Profile",
description: "User profile information"
},
async (uri, { userId }, extra) => ({
contents: [{
uri: uri.href,
text: `Profile data for user ${userId}`
}]
})
);
const client = new Client({ name: "test-client", version: "1.0.0" });
await server.server.connect(serverTransport);
await client.connect(clientTransport);
const resourceTemplates = await client.listResourceTemplates();
expect(resourceTemplates.resourceTemplates).toHaveLength(1);
expect(resourceTemplates.resourceTemplates[0].name).toBe("user-profile");
expect(resourceTemplates.resourceTemplates[0].title).toBe("User Profile");
expect(resourceTemplates.resourceTemplates[0].description).toBe("User profile information");
expect(resourceTemplates.resourceTemplates[0].uriTemplate).toBe("users://{userId}/profile");
// Test reading the resource
const readResult = await client.readResource({ uri: "users://123/profile" });
expect(readResult.contents).toHaveLength(1);
expect(readResult.contents[0].text).toBe("Profile data for user 123");
});
it("should support serverInfo with title", async () => {
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();