Skip to content

Commit c1c2292

Browse files
relax requirements on resource URIs (#365)
remove checks for URI scheme; nothing in the spec requires one # Background When adding resource templates, we are currently doing checks for schemes on absolute uris. It is still common for MCP servers to use custom uris that would not pass these checks. For example, if the official GitHub MCP server were to use this sdk, it would show failures like: ``` "repo://{owner}/{repo}/contents{/path*}": parse "repo://{owner}/{repo}/contents{/path*}": invalid character "{" in host name "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}": parse "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}": invalid character "{" in host name "repo://{owner}/{repo}/sha/{sha}/contents{/path*}": parse "repo://{owner}/{repo}/sha/{sha}/contents{/path*}": invalid character "{" in host name invalid resource template uri "repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}": parse "repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}": invalid character "{" in host name "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}": parse "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}": invalid character "{" in host name ``` The wikipedia MCP would also show problems with missing schemes. ``` "/search/{query}": <nil> "/article/{title}": <nil> "/summary/{title}": <nil> "/summary/{title}/query/{query}/length/{max_length}": <nil> "/summary/{title}/section/{section_title}/length/{max_length}": <nil> "/sections/{title}": <nil> "/links/{title}": <nil> "/facts/{title}/topic/{topic_within_article}/count/{count}": <nil> "/coordinates/{title}": <nil> ```
1 parent 73dd76b commit c1c2292

File tree

2 files changed

+1
-15
lines changed

2 files changed

+1
-15
lines changed

mcp/server.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,9 @@ func (s *Server) RemoveTools(names ...string) {
345345
func (s *Server) AddResource(r *Resource, h ResourceHandler) {
346346
s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{},
347347
func() bool {
348-
u, err := url.Parse(r.URI)
349-
if err != nil {
348+
if _, err := url.Parse(r.URI); err != nil {
350349
panic(err) // url.Parse includes the URI in the error
351350
}
352-
if !u.IsAbs() {
353-
panic(fmt.Errorf("URI %s needs a scheme", r.URI))
354-
}
355351
s.resources.add(&serverResource{r, h})
356352
return true
357353
})
@@ -374,14 +370,6 @@ func (s *Server) AddResourceTemplate(t *ResourceTemplate, h ResourceHandler) {
374370
if err != nil {
375371
panic(fmt.Errorf("URI template %q is invalid: %w", t.URITemplate, err))
376372
}
377-
// Ensure the URI template has a valid scheme
378-
u, err := url.Parse(t.URITemplate)
379-
if err != nil {
380-
panic(err) // url.Parse includes the URI in the error
381-
}
382-
if !u.IsAbs() {
383-
panic(fmt.Errorf("URI template %q needs a scheme", t.URITemplate))
384-
}
385373
s.resourceTemplates.add(&serverResourceTemplate{t, h})
386374
return true
387375
})

mcp/server_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,6 @@ func TestServerAddResourceTemplate(t *testing.T) {
381381
}{
382382
{"ValidFileTemplate", "file:///{a}/{b}", false},
383383
{"ValidCustomScheme", "myproto:///{a}", false},
384-
{"MissingScheme1", "://example.com/{path}", true},
385-
{"MissingScheme2", "/api/v1/users/{id}", true},
386384
{"EmptyVariable", "file:///{}/{b}", true},
387385
{"UnclosedVariable", "file:///{a", true},
388386
}

0 commit comments

Comments
 (0)