Skip to content

Conversation

@ascandone
Copy link
Contributor

@ascandone ascandone commented Oct 7, 2025

Just an initial working MPV of an MCP integration for numscript. There are endless improvement that can be made, by improving current prompts and adding many other tools, but for now that's 80% of the value we can get out of it, with not so much complexity

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 7, 2025

Walkthrough

Adds an MCP server exposing evaluate and check tools for Numscript, a hidden Cobra subcommand to run it, registers the subcommand with the root command, and adds a helper to convert diagnostic severities to plain strings.

Changes

Cohort / File(s) Summary
MCP server and tools
internal/mcp_impl/handlers.go
Adds RunServer() error, registers evaluate and check tools, implements parseBalancesJson and parseVarsJson, tool handlers that validate inputs, run the interpreter or static analysis, and return ToolResultJSON or ToolResultError; serves over stdio.
CLI command wiring
internal/cmd/mcp.go, internal/cmd/root.go
Adds a hidden Cobra subcommand mcp that calls RunServer() (silencing Cobra's automatic error/usage output) and registers it with the root command.
Diagnostics utilities
internal/analysis/diagnostic_kind.go
Adds SeverityToString(Severity) string mapping severity enum values to plain strings ("Error", "Warning", "Info", "Hint").

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant CLI as CLI (cobra root)
  participant MCP as mcpCmd
  participant Server as MCP Server
  participant Tools as Tools (evaluate/check)
  participant Engine as Numscript Engine
  participant Anal as Analyzer

  User->>CLI: run app mcp
  CLI->>MCP: Execute mcpCmd
  MCP->>Server: RunServer()
  Server-->>User: serve over stdio (MCP)

  rect rgb(230,245,255)
    note right of User: evaluate flow
    User->>Server: tool:evaluate {script, balances, vars}
    Server->>Tools: invoke evaluate handler
    Tools->>Tools: parse balances & vars
    Tools->>Engine: interpreter.RunProgram(...)
    alt success
      Engine-->>Tools: result
      Tools-->>Server: ToolResultJSON
      Server-->>User: success response
    else error
      Engine-->>Tools: error
      Tools-->>Server: ToolResultError
      Server-->>User: error response
    end
  end

  rect rgb(240,255,240)
    note right of User: check flow
    User->>Server: tool:check {script}
    Server->>Tools: invoke check handler
    Tools->>Anal: parse + analyze(script)
    Anal-->>Tools: diagnostics [kind,severity,span]
    Tools-->>Server: ToolResultJSON {errors:[...]}
    Server-->>User: response
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~55 minutes

  • Review input validation and ToolResultError shaping in internal/mcp_impl/handlers.go.
  • Verify concurrency/recovery and STDIO server lifecycle in RunServer().
  • Confirm Cobra error/usage silencing in internal/cmd/mcp.go.
  • Check SeverityToString exhaustiveness and alignment with SeverityToAnsiString.

Poem

I hop to the server, nose in the air,
Scripts and balances handled with care.
I parse, I run, I check every string,
Plain severities make my heart sing.
Stdio hums; Numscript dances—hip hop, spring! 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "feat: MCP first MVP" directly corresponds to the main changes in the changeset. The changes implement a complete MCP (Model Context Protocol) server integration for numscript, adding new command structures, handlers, and server bootstrap logic. The title is specific enough to convey the primary change—a new MCP integration being introduced as an MVP—and aligns well with the PR objectives. While the title is somewhat concise, it clearly identifies the feature being added and would be meaningful to someone reviewing the repository history.
Description Check ✅ Passed The PR description clearly relates to the changeset by explicitly describing an "MCP integration for numscript" implemented as an MVP. The description explains the strategic intent—delivering approximately 80% of value with limited complexity—and acknowledges future improvement opportunities such as enhanced prompts and additional tools. This context directly aligns with the changes that introduce MCP server functionality with core tools (evaluate and check) and basic implementation structures, and it appropriately sets expectations for the scope of the initial implementation.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/mcp

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

server.WithInstructions(`
You're a numscript expert AI assistant. Numscript is a DSL that allows modelling finantial transaction in an easy and declarative way. Numscript scripts alwasy terminate.
`),
// TODO add prompt
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can add a more complete prompt in future releases

interpreter.StaticStore{
Balances: balances,
},
map[string]struct{}{},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we'll add feature flags in future releases

@ascandone ascandone requested a review from a team October 7, 2025 08:16
@ascandone ascandone changed the title feat: MCP first MCP feat: MCP first MVP Oct 7, 2025
@codecov
Copy link

codecov bot commented Oct 7, 2025

Codecov Report

❌ Patch coverage is 0% with 148 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.18%. Comparing base (3cf39c3) to head (240e713).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/mcp_impl/handlers.go 0.00% 128 Missing ⚠️
internal/analysis/diagnostic_kind.go 0.00% 12 Missing ⚠️
internal/cmd/mcp.go 0.00% 7 Missing ⚠️
internal/cmd/root.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #97      +/-   ##
==========================================
- Coverage   70.56%   68.18%   -2.39%     
==========================================
  Files          43       45       +2     
  Lines        4142     4290     +148     
==========================================
+ Hits         2923     2925       +2     
- Misses       1061     1208     +147     
+ Partials      158      157       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
internal/cmd/mcp.go (1)

8-22: Set Silence flags on the command itself (optional).

Avoids duplicate error/usage output without toggling inside RunE.

 var mcpCmd = &cobra.Command{
   Use:    "mcp",
   Short:  "Run the mcp server",
   Hidden: true,
+  SilenceErrors: true,
+  SilenceUsage:  true,
   RunE: func(cmd *cobra.Command, args []string) error {
     err := mcp_impl.RunServer()
     if err != nil {
-      cmd.SilenceErrors = true
-      cmd.SilenceUsage = true
       return err
     }
     return nil
   },
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 952c1a7 and de2bf29.

⛔ Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (4)
  • internal/analysis/diagnostic_kind.go (1 hunks)
  • internal/cmd/mcp.go (1 hunks)
  • internal/cmd/root.go (1 hunks)
  • internal/mcp_impl/handlers.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
internal/analysis/diagnostic_kind.go (1)
internal/utils/utils.go (1)
  • NonExhaustiveMatchPanic (8-10)
internal/mcp_impl/handlers.go (3)
internal/interpreter/interpreter.go (1)
  • RunProgram (228-285)
internal/analysis/check.go (1)
  • CheckSource (296-304)
internal/analysis/diagnostic_kind.go (2)
  • SeverityToString (39-52)
  • Severity (12-12)
internal/cmd/mcp.go (1)
internal/mcp_impl/handlers.go (1)
  • RunServer (175-196)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Tests
  • GitHub Check: Dirty
🔇 Additional comments (3)
internal/analysis/diagnostic_kind.go (1)

39-52: Helper looks correct and consistent with existing mapping.

internal/cmd/root.go (1)

27-27: Wiring the hidden mcp subcommand looks good.

internal/mcp_impl/handlers.go (1)

12-14: Confirmed valid mcp-go version pin
go.mod is pinned to v0.41.1, which matches an existing release.

Comment on lines +33 to +41
for asset, amountRaw := range assets {
amount, ok := amountRaw.(float64)
if !ok {
return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected float for amount: %v", amountRaw))
}

n, _ := big.NewFloat(amount).Int(new(big.Int))
iBalances[account][asset] = n
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Amounts parsing risks precision loss and silently truncates fractions. Enforce integer semantics and accept safe types.

  • Converting via big.NewFloat(amount).Int ignores fractional parts and float64 precision limits (money correctness risk).
  • Validate integer-ness, reject NaN/Inf, and optionally accept string integers.

Apply:

-    for asset, amountRaw := range assets {
-      amount, ok := amountRaw.(float64)
-      if !ok {
-        return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected float for amount: %v", amountRaw))
-      }
-
-      n, _ := big.NewFloat(amount).Int(new(big.Int))
-      iBalances[account][asset] = n
-    }
+    for asset, amountRaw := range assets {
+      var n *big.Int
+      switch v := amountRaw.(type) {
+      case float64:
+        if math.IsNaN(v) || math.IsInf(v, 0) {
+          return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Invalid number for amount at %s/%s: %v", account, asset, v))
+        }
+        if v != math.Trunc(v) {
+          return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Amount must be an integer at %s/%s, got: %v", account, asset, v))
+        }
+        if v > float64(math.MaxInt64) || v < float64(math.MinInt64) {
+          return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Amount out of 64-bit range at %s/%s: %v", account, asset, v))
+        }
+        n = big.NewInt(int64(v))
+      case string:
+        n = new(big.Int)
+        if _, ok := n.SetString(v, 10); !ok {
+          return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Invalid integer string for amount at %s/%s: %q", account, asset, v))
+        }
+      default:
+        return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected integer (number or string) for amount at %s/%s, got: %T", account, asset, amountRaw))
+      }
+      iBalances[account][asset] = n
+    }

Additionally, add import:

@@
-import (
+import (
   "context"
   "fmt"
+  "math"
   "math/big"
   "strings"

Committable suggestion skipped: line range outside the PR's diff.

}

out, iErr := interpreter.RunProgram(
context.Background(),
Copy link

Choose a reason for hiding this comment

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

You might want to use the proper context or a derived one ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one should be ok: we are only doing in-memory operations, and fetching data in a go struct
(We need context because the implementor of the interface might be sql queries )

Copy link
Contributor

Choose a reason for hiding this comment

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

David is right.
Always use the correct context.
There is no good reason to not use it.

@ascandone ascandone requested review from Dav-14 and gfyrag October 7, 2025 15:18
@ascandone ascandone enabled auto-merge (squash) October 8, 2025 06:49
@ascandone ascandone disabled auto-merge October 8, 2025 06:49
@ascandone ascandone enabled auto-merge (squash) October 8, 2025 06:49
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (2)
internal/mcp_impl/handlers.go (2)

33-41: Amount parsing still risks precision loss and silent truncation.

This issue was previously flagged but remains unresolved. Converting float64 to big.Int via big.NewFloat silently discards fractional parts and is vulnerable to floating-point precision limits. For financial amounts, this poses data integrity risks.

The previous review provided a comprehensive fix that validates integer-ness, rejects NaN/Inf, and optionally accepts string integers. Please refer to the detailed solution in the earlier review comment.


104-111: Critical: Parse error is created but not returned.

When parse errors are detected, line 110 creates a ToolResultError but doesn't return it. Execution continues to line 113 with an invalid parsed.Value, which will cause incorrect behavior or runtime errors.

This issue was previously flagged and marked as addressed, but the fix was not applied.

Apply this diff:

 	parsed := parser.Parse(script)
 	if len(parsed.Errors) != 0 {
 		out := make([]string, len(parsed.Errors))
 		for index, err := range parsed.Errors {
 			out[index] = err.Msg
 		}
-		mcp.NewToolResultError(strings.Join(out, ", "))
+		return mcp.NewToolResultError(strings.Join(out, ", ")), nil
 	}
🧹 Nitpick comments (3)
internal/cmd/mcp.go (1)

12-21: Simplify error handling logic.

The conditional check is redundant. The silence flags can be set unconditionally before calling RunServer, and you can directly return its result.

Apply this diff:

 	RunE: func(cmd *cobra.Command, args []string) error {
-		err := mcp_impl.RunServer()
-		if err != nil {
-			cmd.SilenceErrors = true
-			cmd.SilenceUsage = true
-			return err
-		}
-
-		return nil
+		cmd.SilenceErrors = true
+		cmd.SilenceUsage = true
+		return mcp_impl.RunServer()
 	},
internal/mcp_impl/handlers.go (2)

159-166: Consider renaming "errors" variable to "diagnostics" for clarity.

The variable is named errors but contains diagnostics of all severity levels (errors, warnings, info, hints). The name is misleading.

Apply this diff:

-	var errors []any
+	var diagnostics []any
 	for _, d := range checkResult.Diagnostics {
-		errors = append(errors, map[string]any{
+		diagnostics = append(diagnostics, map[string]any{
 			"kind":     d.Kind.Message(),
 			"severity": analysis.SeverityToString(d.Kind.Severity()),
 			"span":     d.Range,
 		})
 	}

 	return mcp.NewToolResultJSON(map[string]any{
-		"errors": errors,
+		"diagnostics": diagnostics,
 	})

188-194: Simplify return statement.

The explicit nil return at line 193 is redundant since ServeStdio already returns nil on success.

Apply this diff:

 	// Start the server
-	if err := server.ServeStdio(s); err != nil {
-		return err
-	}
-
-	return nil
+	return server.ServeStdio(s)
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between eb5c0f1 and 240e713.

⛔ Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (4)
  • internal/analysis/diagnostic_kind.go (1 hunks)
  • internal/cmd/mcp.go (1 hunks)
  • internal/cmd/root.go (1 hunks)
  • internal/mcp_impl/handlers.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/analysis/diagnostic_kind.go
🧰 Additional context used
🧬 Code graph analysis (2)
internal/mcp_impl/handlers.go (4)
internal/interpreter/interpreter.go (1)
  • RunProgram (228-285)
internal/analysis/check.go (1)
  • CheckSource (296-304)
internal/analysis/diagnostic_kind.go (2)
  • SeverityToString (39-52)
  • Severity (12-12)
internal/parser/range.go (1)
  • Range (13-16)
internal/cmd/mcp.go (1)
internal/mcp_impl/handlers.go (1)
  • RunServer (174-194)
🔇 Additional comments (2)
internal/cmd/root.go (1)

27-27: LGTM!

The MCP command registration follows the existing pattern and is positioned appropriately between the LSP and check commands.

internal/mcp_impl/handlers.go (1)

123-135: LGTM!

The interpreter execution properly uses the provided context and handles errors correctly. The error guard at lines 132-134 ensures the dereferencing at line 135 is safe.

@ascandone ascandone merged commit 06a7199 into main Oct 28, 2025
5 of 7 checks passed
@ascandone ascandone deleted the feat/mcp branch October 28, 2025 10:11
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.

5 participants