@@ -194,63 +194,39 @@ func TestServerWithResourceTemplate(t *testing.T) {
194194 srv := mcptest .NewUnstartedServer (t )
195195 defer srv .Close ()
196196
197- // Create a URI template for files like "file://users/{userId}/documents/{docId}"
198- uriTemplate := & mcp.URITemplate {}
199- if err := uriTemplate .UnmarshalJSON ([]byte (`"file://users/{userId}/documents/{docId}"` )); err != nil {
200- t .Fatal ("URITemplate.UnmarshalJSON:" , err )
201- }
202-
203- template := mcp.ResourceTemplate {
204- URITemplate : uriTemplate ,
205- Name : "User Document" ,
206- Description : "A user's document" ,
207- MIMEType : "text/plain" ,
208- }
197+ template := mcp .NewResourceTemplate (
198+ "file://users/{userId}/documents/{docId}" ,
199+ "User Document" ,
200+ mcp .WithTemplateDescription ("A user's document" ),
201+ mcp .WithTemplateMIMEType ("text/plain" ),
202+ )
209203
210204 handler := func (ctx context.Context , request mcp.ReadResourceRequest ) ([]mcp.ResourceContents , error ) {
211- // First verify the arguments were correctly extracted from the URI template
212205 if request .Params .Arguments == nil {
213206 return nil , fmt .Errorf ("expected arguments to be populated from URI template" )
214207 }
215208
216- userId , ok := request .Params .Arguments ["userId" ].(string )
209+ userIds , ok := request .Params .Arguments ["userId" ].([] string )
217210 if ! ok {
218211 return nil , fmt .Errorf ("expected userId argument to be populated from URI template" )
219212 }
220- if userId != "john" {
221- return nil , fmt .Errorf ("expected userId argument to be 'john', got %q " , userId )
213+ if len ( userIds ) != 1 && userIds [ 0 ] != "john" {
214+ return nil , fmt .Errorf ("expected userId argument to be 'john', got %v " , userIds )
222215 }
223216
224- docId , ok := request .Params .Arguments ["docId" ].(string )
217+ docIds , ok := request .Params .Arguments ["docId" ].([] string )
225218 if ! ok {
226219 return nil , fmt .Errorf ("expected docId argument to be populated from URI template" )
227220 }
228- if docId != "readme.txt" {
229- return nil , fmt .Errorf ("expected docId argument to be 'readme.txt', got %q" , docId )
230- }
231-
232- // If arguments weren't extracted, parse from URI as fallback
233- if userId == "" || docId == "" {
234- // Parse "file://users/john/documents/readme.txt" to extract john and readme.txt
235- uri := request .Params .URI
236- if len (uri ) > 13 && uri [:13 ] == "file://users/" {
237- parts := uri [13 :] // Remove "file://users/"
238- if idx := strings .Index (parts , "/documents/" ); idx > 0 {
239- userId = parts [:idx ]
240- docId = parts [idx + 11 :] // Remove "/documents/"
241- }
242- }
243- }
244-
245- if userId == "" || docId == "" {
246- return nil , fmt .Errorf ("could not extract userId and docId from URI: %s" , request .Params .URI )
221+ if len (docIds ) != 1 && docIds [0 ] != "readme.txt" {
222+ return nil , fmt .Errorf ("expected docId argument to be 'readme.txt', got %v" , docIds )
247223 }
248224
249225 return []mcp.ResourceContents {
250226 mcp.TextResourceContents {
251227 URI : request .Params .URI ,
252228 MIMEType : "text/plain" ,
253- Text : fmt .Sprintf ("Document %s for user %s" , docId , userId ),
229+ Text : fmt .Sprintf ("Document %s for user %s" , docIds [ 0 ], userIds [ 0 ] ),
254230 },
255231 }, nil
256232 }
0 commit comments