Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

Commit f17c6d4

Browse files
committed
chore: some type annotation
1 parent f7d83b1 commit f17c6d4

2 files changed

Lines changed: 19 additions & 43 deletions

File tree

packages/mcp-use/src/server/mcp-server.ts

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
TemplateDefinition,
66
ToolDefinition,
77
} from './types.js'
8-
import { McpServer as OfficialMcpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
8+
import { McpServer as OfficialMcpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
99
import { z } from 'zod'
1010
import express, { type Express } from 'express'
1111
import { existsSync, readdirSync } from 'node:fs'
@@ -61,15 +61,7 @@ export class McpServer {
6161
description: definition.description,
6262
mimeType: definition.mimeType,
6363
},
64-
async () => ({
65-
contents: [
66-
{
67-
uri: definition.uri,
68-
mimeType: definition.mimeType || 'text/plain',
69-
text: await definition.fn(),
70-
},
71-
],
72-
}),
64+
definition.fn,
7365
)
7466
return this
7567
}
@@ -78,24 +70,16 @@ export class McpServer {
7870
* Define a resource template with parameterized URIs
7971
*/
8072
template(definition: TemplateDefinition): this {
81-
// For templates, we'll register them as tools that return resource content
82-
const toolName = `template_${definition.uriTemplate.replace(/[^a-z0-9]/gi, '_')}`
83-
84-
this.server.tool(
85-
toolName,
86-
definition.description || 'Resource Template',
87-
this.createInputSchema(definition.uriTemplate),
88-
async (params: any) => {
89-
const content = await definition.fn(params as Record<string, string>)
90-
return {
91-
content: [
92-
{
93-
type: 'text',
94-
text: content,
95-
},
96-
],
97-
}
73+
const template = new ResourceTemplate(definition.uriTemplate, { list: undefined })
74+
this.server.resource(
75+
definition.name || definition.uriTemplate,
76+
template,
77+
{
78+
name: definition.name,
79+
description: definition.description,
80+
mimeType: definition.mimeType,
9881
},
82+
definition.fn,
9983
)
10084
return this
10185
}
@@ -130,17 +114,7 @@ export class McpServer {
130114
argsSchema,
131115
async (params: any) => {
132116
const result = await definition.fn(params)
133-
return {
134-
messages: [
135-
{
136-
role: 'user',
137-
content: {
138-
type: 'text',
139-
text: result,
140-
},
141-
},
142-
],
143-
}
117+
return result
144118
},
145119
)
146120
return this

packages/mcp-use/src/server/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'
1+
import type { CallToolResult, GetPromptResult, ReadResourceResult } from '@modelcontextprotocol/sdk/types.js'
22
export interface ServerConfig {
33
name: string
44
version: string
@@ -43,7 +43,9 @@ export interface PromptDefinition {
4343
fn: PromptHandler
4444
}
4545

46-
export type ResourceHandler = () => Promise<string>
47-
export type TemplateHandler = (params: Record<string, string>) => Promise<string>
48-
export type ToolHandler = (params: Record<string, any>) => Promise<CallToolResult>
49-
export type PromptHandler = (params: Record<string, any>) => Promise<string>
46+
import type { Variables } from '@modelcontextprotocol/sdk/shared/uriTemplate.js'
47+
48+
export type ResourceHandler = () => Promise<ReadResourceResult>
49+
export type TemplateHandler = (uri: URL, variables: Variables) => Promise<ReadResourceResult>
50+
export type ToolHandler<TInput = Record<string, any>> = (params: TInput) => Promise<CallToolResult>
51+
export type PromptHandler<TInput = Record<string, any>> = (params: TInput) => Promise<GetPromptResult>

0 commit comments

Comments
 (0)