@@ -5,7 +5,7 @@ import type {
55 TemplateDefinition ,
66 ToolDefinition ,
77} from './types.js'
8- import { McpServer as OfficialMcpServer , ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
8+ import { McpServer as OfficialMcpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
99import { z } from 'zod'
1010import express , { type Express } from 'express'
1111import { existsSync , readdirSync } from 'node:fs'
@@ -61,7 +61,15 @@ export class McpServer {
6161 description : definition . description ,
6262 mimeType : definition . mimeType ,
6363 } ,
64- definition . fn ,
64+ async ( ) => ( {
65+ contents : [
66+ {
67+ uri : definition . uri ,
68+ mimeType : definition . mimeType || 'text/plain' ,
69+ text : await definition . fn ( ) ,
70+ } ,
71+ ] ,
72+ } ) ,
6573 )
6674 return this
6775 }
@@ -70,16 +78,24 @@ export class McpServer {
7078 * Define a resource template with parameterized URIs
7179 */
7280 template ( definition : TemplateDefinition ) : this {
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 ,
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+ }
8198 } ,
82- definition . fn ,
8399 )
84100 return this
85101 }
@@ -114,7 +130,17 @@ export class McpServer {
114130 argsSchema ,
115131 async ( params : any ) => {
116132 const result = await definition . fn ( params )
117- return result
133+ return {
134+ messages : [
135+ {
136+ role : 'user' ,
137+ content : {
138+ type : 'text' ,
139+ text : result ,
140+ } ,
141+ } ,
142+ ] ,
143+ }
118144 } ,
119145 )
120146 return this
0 commit comments