-
Notifications
You must be signed in to change notification settings - Fork 4
feat: MCP first MVP #97
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
78f3a7e
draft
ascandone 1d34f59
simplify
ascandone 14316bc
show all errs
ascandone 5da9791
handle vars
ascandone f5d731c
add check
ascandone a0718eb
add instructions
ascandone 95c0aa2
removed comment
ascandone c3e2ab0
fix: fix context
ascandone a07c41f
fix typo
ascandone 240e713
better err
ascandone 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
Some comments aren't visible on the classic Files Changed page.
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,22 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/formancehq/numscript/internal/mcp_impl" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var mcpCmd = &cobra.Command{ | ||
| Use: "mcp", | ||
| Short: "Run the mcp server", | ||
| Hidden: 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 | ||
| }, | ||
| } |
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,194 @@ | ||
| package mcp_impl | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "math/big" | ||
| "strings" | ||
|
|
||
| "github.com/formancehq/numscript/internal/analysis" | ||
| "github.com/formancehq/numscript/internal/interpreter" | ||
| "github.com/formancehq/numscript/internal/parser" | ||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/mark3labs/mcp-go/server" | ||
| ) | ||
|
|
||
| func parseBalancesJson(balancesRaw any) (interpreter.Balances, *mcp.CallToolResult) { | ||
| balances, ok := balancesRaw.(map[string]any) | ||
| if !ok { | ||
| return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected an object as balances, got: <%#v>", balancesRaw)) | ||
| } | ||
|
|
||
| iBalances := interpreter.Balances{} | ||
| for account, assetsRaw := range balances { | ||
| if iBalances[account] == nil { | ||
| iBalances[account] = interpreter.AccountBalance{} | ||
| } | ||
|
|
||
| assets, ok := assetsRaw.(map[string]any) | ||
| if !ok { | ||
| return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected nested object for account %v", account)) | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| return iBalances, nil | ||
| } | ||
|
|
||
| func parseVarsJson(varsRaw any) (map[string]string, *mcp.CallToolResult) { | ||
| vars, ok := varsRaw.(map[string]any) | ||
| if !ok { | ||
| return map[string]string{}, mcp.NewToolResultError(fmt.Sprintf("Expected an object as vars, got: <%#v>", varsRaw)) | ||
| } | ||
|
|
||
| iVars := map[string]string{} | ||
| for key, rawValue := range vars { | ||
|
|
||
| value, ok := rawValue.(string) | ||
| if !ok { | ||
| return map[string]string{}, mcp.NewToolResultError(fmt.Sprintf("Expected %s var to be a string, got: %T instead", key, rawValue)) | ||
| } | ||
|
|
||
| iVars[key] = value | ||
| } | ||
|
|
||
| return iVars, nil | ||
| } | ||
|
|
||
| func addEvalTool(s *server.MCPServer) { | ||
| tool := mcp.NewTool("evaluate", | ||
| mcp.WithDescription("Evaluate a numscript program"), | ||
| mcp.WithIdempotentHintAnnotation(true), | ||
| mcp.WithReadOnlyHintAnnotation(true), | ||
| mcp.WithOpenWorldHintAnnotation(false), | ||
| mcp.WithString("script", | ||
| mcp.Required(), | ||
| mcp.Description("The numscript source"), | ||
| ), | ||
| mcp.WithObject("balances", | ||
| mcp.Required(), | ||
| mcp.Description(`The accounts' balances. A nested map from the account name, to the asset, to its integer amount. | ||
| For example: { "alice": { "USD/2": 100, "EUR/2": -42 }, "bob": { "BTC": 1 } } | ||
| `), | ||
| ), | ||
| mcp.WithObject("vars", | ||
| mcp.Required(), | ||
| mcp.Description(`The stringified variables to be passed to the script's "vars" block. | ||
| For example: { "acc": "alice", "mon": "EUR 100" } can be passed to the following script: | ||
| vars { | ||
| monetary $mon | ||
| account $acc | ||
| } | ||
|
|
||
| send $mon ( | ||
| source = $acc | ||
| destination = @world | ||
| ) | ||
| `), | ||
| ), | ||
| ) | ||
| s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
| script, err := request.RequireString("script") | ||
| if err != nil { | ||
| return mcp.NewToolResultError(err.Error()), nil | ||
| } | ||
|
|
||
| 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, ", ")) | ||
| } | ||
|
|
||
| balances, mcpErr := parseBalancesJson(request.GetArguments()["balances"]) | ||
| if mcpErr != nil { | ||
| return mcpErr, nil | ||
| } | ||
|
|
||
| vars, mcpErr := parseVarsJson(request.GetArguments()["vars"]) | ||
| if mcpErr != nil { | ||
| return mcpErr, nil | ||
| } | ||
|
|
||
| out, iErr := interpreter.RunProgram( | ||
| ctx, | ||
| parsed.Value, | ||
| vars, | ||
| interpreter.StaticStore{ | ||
| Balances: balances, | ||
| }, | ||
| map[string]struct{}{}, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'll add feature flags in future releases |
||
| ) | ||
| if iErr != nil { | ||
| return mcp.NewToolResultError(iErr.Error()), nil | ||
| } | ||
| return mcp.NewToolResultJSON(*out) | ||
| }) | ||
| } | ||
|
|
||
| func addCheckTool(s *server.MCPServer) { | ||
| tool := mcp.NewTool("check", | ||
| mcp.WithDescription("Check a program for parsing error or static analysis errors"), | ||
| mcp.WithIdempotentHintAnnotation(true), | ||
| mcp.WithReadOnlyHintAnnotation(true), | ||
| mcp.WithOpenWorldHintAnnotation(false), | ||
| mcp.WithString("script", | ||
| mcp.Required(), | ||
| mcp.Description("The numscript source"), | ||
| ), | ||
| ) | ||
|
|
||
| s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
| script, err := request.RequireString("script") | ||
| if err != nil { | ||
| return mcp.NewToolResultError(err.Error()), nil | ||
| } | ||
|
|
||
| checkResult := analysis.CheckSource(script) | ||
|
|
||
| var errors []any | ||
| for _, d := range checkResult.Diagnostics { | ||
| errors = append(errors, map[string]any{ | ||
| "kind": d.Kind.Message(), | ||
| "severity": analysis.SeverityToString(d.Kind.Severity()), | ||
| "span": d.Range, | ||
| }) | ||
| } | ||
|
|
||
| return mcp.NewToolResultJSON(map[string]any{ | ||
| "errors": errors, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func RunServer() error { | ||
| // Create a new MCP server | ||
| s := server.NewMCPServer( | ||
| "Numscript", | ||
| "0.0.1", | ||
| server.WithToolCapabilities(false), | ||
| server.WithRecovery(), | ||
| server.WithInstructions(` | ||
| You're a Numscript expert AI assistant. Numscript is a DSL that allows modeling financial transactions in an easy and declarative way. Numscript scripts always terminate. | ||
| `), | ||
| ) | ||
| addEvalTool(s) | ||
| addCheckTool(s) | ||
|
|
||
| // Start the server | ||
| if err := server.ServeStdio(s); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.
Amounts parsing risks precision loss and silently truncates fractions. Enforce integer semantics and accept safe types.
Apply:
Additionally, add import: