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>
```
This commit is contained in:
parent
73dd76b614
commit
c1c22925da
2 changed files with 1 additions and 15 deletions
|
|
@ -345,13 +345,9 @@ func (s *Server) RemoveTools(names ...string) {
|
|||
func (s *Server) AddResource(r *Resource, h ResourceHandler) {
|
||||
s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{},
|
||||
func() bool {
|
||||
u, err := url.Parse(r.URI)
|
||||
if err != nil {
|
||||
if _, err := url.Parse(r.URI); err != nil {
|
||||
panic(err) // url.Parse includes the URI in the error
|
||||
}
|
||||
if !u.IsAbs() {
|
||||
panic(fmt.Errorf("URI %s needs a scheme", r.URI))
|
||||
}
|
||||
s.resources.add(&serverResource{r, h})
|
||||
return true
|
||||
})
|
||||
|
|
@ -374,14 +370,6 @@ func (s *Server) AddResourceTemplate(t *ResourceTemplate, h ResourceHandler) {
|
|||
if err != nil {
|
||||
panic(fmt.Errorf("URI template %q is invalid: %w", t.URITemplate, err))
|
||||
}
|
||||
// Ensure the URI template has a valid scheme
|
||||
u, err := url.Parse(t.URITemplate)
|
||||
if err != nil {
|
||||
panic(err) // url.Parse includes the URI in the error
|
||||
}
|
||||
if !u.IsAbs() {
|
||||
panic(fmt.Errorf("URI template %q needs a scheme", t.URITemplate))
|
||||
}
|
||||
s.resourceTemplates.add(&serverResourceTemplate{t, h})
|
||||
return true
|
||||
})
|
||||
|
|
|
|||
|
|
@ -381,8 +381,6 @@ func TestServerAddResourceTemplate(t *testing.T) {
|
|||
}{
|
||||
{"ValidFileTemplate", "file:///{a}/{b}", false},
|
||||
{"ValidCustomScheme", "myproto:///{a}", false},
|
||||
{"MissingScheme1", "://example.com/{path}", true},
|
||||
{"MissingScheme2", "/api/v1/users/{id}", true},
|
||||
{"EmptyVariable", "file:///{}/{b}", true},
|
||||
{"UnclosedVariable", "file:///{a", true},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue