Skip to content

add overloads for registerResource method in McpServer class #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kentcdodds
Copy link

Motivation and Context

Without the overload, TypeScript struggles to distinguish between ReadResourceCallback and ReadResourceTemplateCallback.

How Has This Been Tested?

Locally modified the sdk and verified the typings work as expected with this change.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Here's a real-world example that has issues without this change:

	agent.server.registerResource(
		'entry',
		new ResourceTemplate('epicme://entries/{id}', {
			complete: {
				async id(value) {
					const user = await agent.requireUser()
					const entries = await agent.db.getEntries(user.id)
					return entries
						.map((entry) => entry.id.toString())
						.filter((id) => id.includes(value))
				},
			},
			list: async () => {
				const user = await agent.requireUser()
				const entries = await agent.db.getEntries(user.id)
				return {
					resources: entries.map((entry) => ({
						name: entry.title,
						uri: `epicme://entries/${entry.id}`,
						mimeType: 'application/json',
					})),
				}
			},
		}),
		{ description: 'A journal entry' },
		async (uri: URL, { id }) => {
			const user = await agent.requireUser()
			const entry = await agent.db.getEntry(user.id, Number(id))
			invariant(entry, `Entry with ID "${id}" not found`)
			return {
				contents: [
					{
						mimeType: 'application/json',
						uri: uri.toString(),
						text: JSON.stringify(entry),
					},
				],
			}
		},
	)

Here's the type error:

Argument of type '(uri: URL, { id }: { id: any; }) => Promise<{ contents: { mimeType: string; uri: string; text: string; }[]; }>' is not assignable to parameter of type 'ReadResourceCallback | ReadResourceTemplateCallback'.
  Type '(uri: URL, { id }: { id: any; }) => Promise<{ contents: { mimeType: string; uri: string; text: string; }[]; }>' is not assignable to type 'ReadResourceCallback'.
    Types of parameters '__1' and 'extra' are incompatible.
      Property 'id' is missing in type 'RequestHandlerExtra<ServerRequest, ServerNotification>' but required in type '{ id: any; }'.ts(2345)

I can manually add satisfies ReadResourceTemplateCallback to the callback, but that should not be necessary and this change removes the issue entirely.

Otherwise, TypeScript can't properly distinguish the type of callback
@kentcdodds
Copy link
Author

I've published this as @kentcdodds/[email protected] for anyone who wants to test it out easily.

@ctjlewis
Copy link

Good PR. Ideally we could type the params for registerResource with { inputSchema } like with registerTool also... the example program throws because { name } is never typed.

@kentcdodds
Copy link
Author

Ideally we could type the params

Doing this would be a significantly bigger effort and out of scope for this PR. This PR is interested in just making it so the typical use functions at all. I would like to get this merged as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants