-
Notifications
You must be signed in to change notification settings - Fork 4.2k
test(github): enforce explicit ReadOnlyHint on every mcp.Tool literal #2486
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
Open
jluocsa
wants to merge
2
commits into
github:main
Choose a base branch
from
jluocsa:feat/lint-readonly-hint-annotation-2483
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestAllToolRegistrationsExplicitlySetReadOnlyHint statically scans every | ||
| // non-test Go source file in this package and asserts that every mcp.Tool | ||
| // composite literal explicitly sets Annotations.ReadOnlyHint. | ||
| // | ||
| // This complements TestAllToolsHaveRequiredMetadata, which can only check | ||
| // that Annotations is non-nil at runtime: Go cannot distinguish an | ||
| // unset bool field from one explicitly set to false. Source-level | ||
| // validation closes that gap and prevents future tool registrations | ||
| // from silently defaulting ReadOnlyHint to false (which has caused | ||
| // downstream agents to prompt for human approval on read-intent tools). | ||
| // | ||
| // Related issue: github/github-mcp-server#2483 | ||
| func TestAllToolRegistrationsExplicitlySetReadOnlyHint(t *testing.T) { | ||
| pkgDir, err := os.Getwd() | ||
| require.NoError(t, err, "must be able to resolve package directory") | ||
|
|
||
| fset := token.NewFileSet() | ||
| pkgs, err := parser.ParseDir(fset, pkgDir, func(info os.FileInfo) bool { | ||
| // Skip test files: they are allowed to construct mcp.Tool literals | ||
| // for fixtures or mocks where ReadOnlyHint is not meaningful. | ||
| return !strings.HasSuffix(info.Name(), "_test.go") | ||
| }, parser.ParseComments) | ||
| require.NoError(t, err, "parser.ParseDir on package directory") | ||
| require.NotEmpty(t, pkgs, "expected at least one package parsed") | ||
|
|
||
| type violation struct { | ||
| file string | ||
| line int | ||
| toolName string | ||
| reason string | ||
| } | ||
| var violations []violation | ||
| literalsSeen := 0 | ||
|
|
||
| for _, pkg := range pkgs { | ||
| for filename, file := range pkg.Files { | ||
| ast.Inspect(file, func(n ast.Node) bool { | ||
| cl, ok := n.(*ast.CompositeLit) | ||
| if !ok { | ||
| return true | ||
| } | ||
| if !isMCPToolType(cl.Type) { | ||
| return true | ||
| } | ||
| literalsSeen++ | ||
|
|
||
| toolName := extractToolName(cl) | ||
| if toolName == "" { | ||
| toolName = "<unknown>" | ||
| } | ||
| pos := fset.Position(cl.Pos()) | ||
| rel, _ := filepath.Rel(pkgDir, filename) | ||
| if rel == "" { | ||
| rel = filepath.Base(filename) | ||
| } | ||
|
|
||
| annotations := findFieldValue(cl, "Annotations") | ||
| if annotations == nil { | ||
| violations = append(violations, violation{ | ||
| file: rel, | ||
| line: pos.Line, | ||
| toolName: toolName, | ||
| reason: "mcp.Tool literal is missing an Annotations field", | ||
| }) | ||
| return true | ||
| } | ||
|
|
||
| annoLit := unwrapAnnotationsLiteral(annotations) | ||
| if annoLit == nil { | ||
| // Annotations is set to something we can't statically | ||
| // verify (e.g. a function call). Flag it so reviewers | ||
| // can confirm ReadOnlyHint is honored. | ||
| violations = append(violations, violation{ | ||
| file: rel, | ||
| line: pos.Line, | ||
| toolName: toolName, | ||
| reason: "Annotations is not an &mcp.ToolAnnotations{...} literal; ReadOnlyHint cannot be statically verified", | ||
| }) | ||
| return true | ||
| } | ||
|
|
||
| if findFieldValue(annoLit, "ReadOnlyHint") == nil { | ||
| violations = append(violations, violation{ | ||
| file: rel, | ||
| line: pos.Line, | ||
| toolName: toolName, | ||
| reason: "ToolAnnotations literal does not explicitly set ReadOnlyHint", | ||
| }) | ||
| } | ||
| return true | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| require.NotZero(t, literalsSeen, | ||
| "expected to discover at least one mcp.Tool literal; AST walker may be broken") | ||
|
|
||
| if len(violations) > 0 { | ||
| var msg strings.Builder | ||
| msg.WriteString("Found tool registrations that do not explicitly set ReadOnlyHint:\n") | ||
| for _, v := range violations { | ||
| msg.WriteString(" - ") | ||
| msg.WriteString(v.file) | ||
| msg.WriteString(":") | ||
| msg.WriteString(strconv.Itoa(v.line)) | ||
| msg.WriteString(" tool=") | ||
| msg.WriteString(v.toolName) | ||
| msg.WriteString(": ") | ||
| msg.WriteString(v.reason) | ||
| msg.WriteString("\n") | ||
| } | ||
| msg.WriteString("\nEvery mcp.Tool registration must declare Annotations.ReadOnlyHint explicitly ") | ||
| msg.WriteString("(true for read-only tools, false for tools with side effects). ") | ||
| msg.WriteString("See pkg/github/tools_static_validation_test.go.") | ||
| t.Fatal(msg.String()) | ||
| } | ||
| } | ||
|
|
||
| // isMCPToolType reports whether the given AST expression refers to mcp.Tool. | ||
| func isMCPToolType(expr ast.Expr) bool { | ||
| sel, ok := expr.(*ast.SelectorExpr) | ||
| if !ok { | ||
| return false | ||
| } | ||
| ident, ok := sel.X.(*ast.Ident) | ||
| if !ok { | ||
| return false | ||
| } | ||
| return ident.Name == "mcp" && sel.Sel != nil && sel.Sel.Name == "Tool" | ||
| } | ||
|
|
||
| // isMCPToolAnnotationsType reports whether the given AST expression refers to mcp.ToolAnnotations. | ||
| func isMCPToolAnnotationsType(expr ast.Expr) bool { | ||
| sel, ok := expr.(*ast.SelectorExpr) | ||
| if !ok { | ||
| return false | ||
| } | ||
| ident, ok := sel.X.(*ast.Ident) | ||
| if !ok { | ||
| return false | ||
| } | ||
| return ident.Name == "mcp" && sel.Sel != nil && sel.Sel.Name == "ToolAnnotations" | ||
| } | ||
|
|
||
|
jluocsa marked this conversation as resolved.
Outdated
|
||
| // findFieldValue returns the value expression for the named keyed field of a | ||
| // composite literal, or nil if the field is absent. | ||
| func findFieldValue(cl *ast.CompositeLit, name string) ast.Expr { | ||
| for _, elt := range cl.Elts { | ||
| kv, ok := elt.(*ast.KeyValueExpr) | ||
| if !ok { | ||
| continue | ||
| } | ||
| key, ok := kv.Key.(*ast.Ident) | ||
| if !ok { | ||
| continue | ||
| } | ||
| if key.Name == name { | ||
| return kv.Value | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
jluocsa marked this conversation as resolved.
|
||
|
|
||
| // unwrapAnnotationsLiteral attempts to extract the *ast.CompositeLit for | ||
| // &mcp.ToolAnnotations{...} or mcp.ToolAnnotations{...} from an expression. | ||
| // Returns nil if the expression is not a statically inspectable literal. | ||
| func unwrapAnnotationsLiteral(expr ast.Expr) *ast.CompositeLit { | ||
| if u, ok := expr.(*ast.UnaryExpr); ok && u.Op == token.AND { | ||
| expr = u.X | ||
| } | ||
| cl, ok := expr.(*ast.CompositeLit) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| if !isMCPToolAnnotationsType(cl.Type) { | ||
| return nil | ||
| } | ||
| return cl | ||
| } | ||
|
|
||
| // extractToolName returns the literal value of the Name field of an mcp.Tool | ||
| // composite literal, or empty string if the value is not a basic string literal. | ||
| func extractToolName(cl *ast.CompositeLit) string { | ||
| v := findFieldValue(cl, "Name") | ||
| if v == nil { | ||
| return "" | ||
| } | ||
| bl, ok := v.(*ast.BasicLit) | ||
| if !ok || bl.Kind != token.STRING { | ||
| return "" | ||
| } | ||
| // Strip surrounding quotes; tolerate raw strings too. | ||
| s := bl.Value | ||
| if len(s) >= 2 && (s[0] == '"' || s[0] == '`') { | ||
| s = s[1 : len(s)-1] | ||
|
jluocsa marked this conversation as resolved.
Outdated
|
||
| } | ||
| return s | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.