-
Notifications
You must be signed in to change notification settings - Fork 175
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
Conversation
@samthanawalla could you help review? |
mcp/server.go
Outdated
// 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)) |
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
mcp/server.go
Outdated
panic(err) // url.Parse includes the URI in the error | ||
} | ||
if !u.IsAbs() { | ||
panic(fmt.Errorf("URI template %s needs a scheme", t.URITemplate)) |
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.
use %q instead of %s to quote the string
mcp/server.go
Outdated
// 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)) |
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.
use %q instead of %s to quote the string
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.
All done. Thank you for the review!
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.
Thanks!
if !tt.expectPanic { | ||
t.Errorf("%s: unexpected panic: %v", tt.name, r) | ||
} | ||
} else { |
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.
else if
…ntextprotocol#253) Add template validation and scheme check in `AddResourceTemplate`.
change:
To avoid silent failures caused by invalid URI template, this PR:
AddResourceTemplate
, panic if the template is invalid or lacks a schemeWe recently encountered an internal issue where a malformed template was mistakenly added. It could never match any URI, and since the current
Matches
implementation returnsfalse
on parse errors without logging, it silently failed and made debugging harder.If this validation is not aligned with intended behavior (e.g. if template correctness is considered caller responsibility), happy to close this PR. Just wanted to raise it after hitting a subtle failure in practice.
go-sdk/mcp/resource.go
Lines 157 to 164 in b392875