-
Notifications
You must be signed in to change notification settings - Fork 284
mcp: add syntax and scheme validation to AddResourceTemplate #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import ( | |
| "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" | ||
| "github.com/modelcontextprotocol/go-sdk/internal/util" | ||
| "github.com/modelcontextprotocol/go-sdk/jsonrpc" | ||
| "github.com/yosida95/uritemplate/v3" | ||
| ) | ||
|
|
||
| const DefaultPageSize = 1000 | ||
|
|
@@ -220,7 +221,19 @@ func (s *Server) RemoveResources(uris ...string) { | |
| func (s *Server) AddResourceTemplate(t *ResourceTemplate, h ResourceHandler) { | ||
| s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{}, | ||
| func() bool { | ||
| // TODO: check template validity. | ||
| // Validate the URI template syntax | ||
| _, err := uritemplate.New(t.URITemplate) | ||
| if err != nil { | ||
| panic(fmt.Errorf("URI template %s is invalid: %v", 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 %s needs a scheme", t.URITemplate)) | ||
|
||
| } | ||
| s.resourceTemplates.add(&serverResourceTemplate{t, h}) | ||
| return true | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -371,3 +371,39 @@ func TestServerCapabilities(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestServerAddResourceTemplate(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| template string | ||
| expectPanic bool | ||
| }{ | ||
| {"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}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| rt := ResourceTemplate{URITemplate: tt.template} | ||
|
|
||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| if !tt.expectPanic { | ||
| t.Errorf("%s: unexpected panic: %v", tt.name, r) | ||
| } | ||
| } else { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. else if |
||
| if tt.expectPanic { | ||
| t.Errorf("%s: expected panic but did not panic", tt.name) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| s := NewServer(testImpl, nil) | ||
| s.AddResourceTemplate(&rt, nil) | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrap the error with %w instead of %v