@@ -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'
99import { z } from 'zod'
1010import express , { type Express } from 'express'
1111import { 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 - z 0 - 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
0 commit comments