Skip to content

Conversation

cryo-zd
Copy link
Contributor

@cryo-zd cryo-zd commented Aug 6, 2025

change:

To avoid silent failures caused by invalid URI template, this PR:

  1. Add template validation and scheme check in AddResourceTemplate, panic if the template is invalid or lacks a scheme
  2. Add test cases

We recently encountered an internal issue where a malformed template was mistakenly added. It could never match any URI, and since the current Matches implementation returns false 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

// Matches reports whether the receiver's uri template matches the uri.
func (sr *serverResourceTemplate) Matches(uri string) bool {
tmpl, err := uritemplate.New(sr.resourceTemplate.URITemplate)
if err != nil {
return false
}
return tmpl.Regexp().MatchString(uri)
}

@findleyr findleyr requested a review from jba August 8, 2025 15:50
@findleyr findleyr requested a review from samthanawalla August 19, 2025 13:44
@findleyr
Copy link
Contributor

@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))
Copy link
Contributor

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))
Copy link
Contributor

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))
Copy link
Contributor

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

Copy link
Contributor Author

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!

Copy link
Contributor

@jba jba left a 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if

@jba jba merged commit 732b97f into modelcontextprotocol:main Aug 19, 2025
5 checks passed
@cryo-zd cryo-zd deleted the validTemplate branch August 20, 2025 16:26
yasomaru pushed a commit to yasomaru/go-sdk that referenced this pull request Aug 28, 2025
…ntextprotocol#253)

Add template validation and scheme check in `AddResourceTemplate`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants