-
Notifications
You must be signed in to change notification settings - Fork 82
Add flow mcp command for Cadence MCP server #2306
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
peterargue
wants to merge
23
commits into
master
Choose a base branch
from
peter/mcp-server
base: master
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.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
12a7036
Add design spec for flow mcp command
peterargue 1ba847c
Soften code review language in MCP design spec
peterargue 69257af
Clarify LSP document lifecycle in MCP design spec
peterargue 3477f02
Add implementation plan for flow mcp command
peterargue 02c5723
Add flow mcp command scaffold with mcp-go dependency
peterargue 67b6f24
Add LSP wrapper for in-process Cadence language server
peterargue 773a2bf
Add tool handler implementations and wire into MCP server
peterargue 75fac3e
Add tool handler tests
peterargue 14bf6b0
Add integration tests for network tools
peterargue 24683bb
Fix integration test expectations for FlowToken address
peterargue f6743fd
Add code review rules for cadence_code_review tool
peterargue 8d28827
Upgrade buger/jsonparser to v1.1.2 to fix CVE
peterargue 4d204ae
Remove planning docs from PR
peterargue 4cdf25c
Add file parameter as alternative to code for all Cadence tools
peterargue dd3688b
Add t.Parallel() to all tests
peterargue 501221e
Replace diagnostic append with replace, filter by scratch URI
peterargue c1cd223
Warn on invalid flow.json instead of silently falling back
peterargue 9a69034
Validate Flow address to prevent silent empty address queries
peterargue 3aeaab3
Use FindAllStringSubmatch to catch multiple matches per line
peterargue b5e2b7a
Use secure gRPC gateway when network has a configured key
peterargue b4eadb7
Remove unused network param from LSP wrapper and tool schemas
peterargue 0609dd3
Remove file parameter from tools to prevent sandbox bypass
peterargue 1281ee7
Fix goimports formatting
peterargue 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
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
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
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,203 @@ | ||
| /* | ||
| * Flow CLI | ||
| * | ||
| * Copyright Flow Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package mcp | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Severity represents the severity level of a code review finding. | ||
| type Severity string | ||
|
|
||
| const ( | ||
| SeverityWarning Severity = "warning" | ||
| SeverityNote Severity = "note" | ||
| SeverityInfo Severity = "info" | ||
| ) | ||
|
|
||
| // Finding represents a single code review finding. | ||
| type Finding struct { | ||
| Rule string `json:"rule"` | ||
| Severity Severity `json:"severity"` | ||
| Line int `json:"line"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| // ReviewResult holds all findings from a code review along with a summary. | ||
| type ReviewResult struct { | ||
| Findings []Finding `json:"findings"` | ||
| Summary map[string]int `json:"summary"` | ||
| } | ||
|
|
||
| // reviewRule defines a single regex-based code review rule. | ||
| type reviewRule struct { | ||
| id string | ||
| severity Severity | ||
| pattern *regexp.Regexp | ||
| message func(match []string) string | ||
| } | ||
|
|
||
| var addressImportPattern = regexp.MustCompile(`^\s*import\s+\w[\w, ]*\s+from\s+0x`) | ||
|
|
||
| var reviewRules = []reviewRule{ | ||
| { | ||
| id: "overly-permissive-access", | ||
| severity: SeverityWarning, | ||
| pattern: regexp.MustCompile(`access\(all\)\s+(var|let)\s+`), | ||
| message: func(_ []string) string { | ||
| return "State field with access(all) — consider restricting access with entitlements" | ||
| }, | ||
| }, | ||
| { | ||
| id: "overly-permissive-function", | ||
| severity: SeverityNote, | ||
| pattern: regexp.MustCompile(`access\(all\)\s+fun\s+(\w+)`), | ||
| message: func(match []string) string { | ||
| name := "" | ||
| if len(match) > 1 { | ||
| name = match[1] | ||
| } | ||
| return fmt.Sprintf("Function '%s' has access(all) — review if public access is intended", name) | ||
| }, | ||
| }, | ||
| { | ||
| id: "deprecated-pub", | ||
| severity: SeverityInfo, | ||
| pattern: regexp.MustCompile(`\bpub\s+(var|let|fun|resource|struct|event|contract|enum)\b`), | ||
| message: func(_ []string) string { | ||
| return "`pub` is deprecated in Cadence 1.0 — use `access(all)` or a more restrictive access modifier" | ||
| }, | ||
| }, | ||
| { | ||
| id: "unsafe-force-unwrap", | ||
| severity: SeverityNote, | ||
| pattern: regexp.MustCompile(`[)\w]\s*!`), | ||
| message: func(_ []string) string { | ||
| return "Force-unwrap (!) used — consider nil-coalescing (??) or optional binding for safer handling" | ||
| }, | ||
| }, | ||
| { | ||
| id: "auth-account-exposure", | ||
| severity: SeverityWarning, | ||
| pattern: regexp.MustCompile(`\bAuthAccount\b`), | ||
| message: func(_ []string) string { | ||
| return "AuthAccount reference found — passing AuthAccount gives full account access, use capabilities instead" | ||
| }, | ||
| }, | ||
| { | ||
| id: "auth-reference-exposure", | ||
| severity: SeverityWarning, | ||
| pattern: regexp.MustCompile(`\bauth\s*\(.*?\)\s*&Account\b`), | ||
| message: func(_ []string) string { | ||
| return "auth(…) &Account reference found — this grants broad account access, prefer scoped capabilities" | ||
| }, | ||
| }, | ||
| { | ||
| id: "hardcoded-address", | ||
| severity: SeverityInfo, | ||
| pattern: regexp.MustCompile(`0x[0-9a-fA-F]{8,16}\b`), | ||
| message: func(_ []string) string { | ||
| return "Hardcoded address detected — consider using named address imports for portability" | ||
| }, | ||
| }, | ||
| { | ||
| id: "unguarded-capability", | ||
| severity: SeverityWarning, | ||
| pattern: regexp.MustCompile(`\.publish\s*\(`), | ||
| message: func(_ []string) string { | ||
| return "Capability published — verify that proper entitlements guard this capability" | ||
| }, | ||
| }, | ||
| { | ||
| id: "resource-loss-destroy", | ||
| severity: SeverityWarning, | ||
| pattern: regexp.MustCompile(`destroy\s*\(`), | ||
| message: func(_ []string) string { | ||
| return "Explicit destroy call — ensure the resource is intentionally being destroyed and not lost" | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // codeReview runs all rules against the provided Cadence source code and returns | ||
| // a ReviewResult with findings sorted by line number. | ||
| func codeReview(code string) ReviewResult { | ||
| lines := strings.Split(code, "\n") | ||
| var findings []Finding | ||
|
|
||
| for lineIdx, line := range lines { | ||
| lineNum := lineIdx + 1 | ||
| for _, rule := range reviewRules { | ||
| // Special case: skip hardcoded-address on import-from-address lines. | ||
| if rule.id == "hardcoded-address" && addressImportPattern.MatchString(line) { | ||
| continue | ||
| } | ||
|
|
||
| matches := rule.pattern.FindAllStringSubmatch(line, -1) | ||
| for _, match := range matches { | ||
| findings = append(findings, Finding{ | ||
| Rule: rule.id, | ||
| Severity: rule.severity, | ||
| Line: lineNum, | ||
| Message: rule.message(match), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sort.Slice(findings, func(i, j int) bool { | ||
| return findings[i].Line < findings[j].Line | ||
| }) | ||
|
|
||
| summary := map[string]int{ | ||
| string(SeverityWarning): 0, | ||
| string(SeverityNote): 0, | ||
| string(SeverityInfo): 0, | ||
| } | ||
| for _, f := range findings { | ||
| summary[string(f.Severity)]++ | ||
| } | ||
|
|
||
| return ReviewResult{ | ||
| Findings: findings, | ||
| Summary: summary, | ||
| } | ||
| } | ||
|
|
||
| // formatReviewResult formats a ReviewResult as human-readable text. | ||
| func formatReviewResult(result ReviewResult) string { | ||
| if len(result.Findings) == 0 { | ||
| return "No findings.\n" | ||
| } | ||
|
|
||
| var sb strings.Builder | ||
| for _, f := range result.Findings { | ||
| sb.WriteString(fmt.Sprintf("[%s] line %d (%s): %s\n", f.Severity, f.Line, f.Rule, f.Message)) | ||
| } | ||
|
|
||
| sb.WriteString(fmt.Sprintf("\nSummary: %d warning(s), %d note(s), %d info(s)\n", | ||
| result.Summary[string(SeverityWarning)], | ||
| result.Summary[string(SeverityNote)], | ||
| result.Summary[string(SeverityInfo)], | ||
| )) | ||
|
|
||
| return sb.String() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Adding auditing functionality is a good idea, it would be nice to have this usable outside of MCP.
Could we maybe move this into a new analyzer in the linter (https://github.com/onflow/cadence-tools/tree/master/lint), or a new tool in https://github.com/onflow/cadence-tools?
The rules should probably also not be regular expression-based, but instead be AST based. Some of these rules are also duplicates of existing linter analyzers (e.g. deprecated pre-1.0 code) and Cadence type checking diagnostics.
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.
I'm not familiar with that system, but I asked claude to open a PR. can you take a look?
onflow/cadence-tools#614
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.
I let claude decide which of the analyzers would be a good contribution to add. let me know if you think we should add/remove any.