|
| 1 | +package mcp_impl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/formancehq/numscript/internal/analysis" |
| 10 | + "github.com/formancehq/numscript/internal/interpreter" |
| 11 | + "github.com/formancehq/numscript/internal/parser" |
| 12 | + "github.com/mark3labs/mcp-go/mcp" |
| 13 | + "github.com/mark3labs/mcp-go/server" |
| 14 | +) |
| 15 | + |
| 16 | +func parseBalancesJson(balancesRaw any) (interpreter.Balances, *mcp.CallToolResult) { |
| 17 | + balances, ok := balancesRaw.(map[string]any) |
| 18 | + if !ok { |
| 19 | + return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected an object as balances, got: <%#v>", balancesRaw)) |
| 20 | + } |
| 21 | + |
| 22 | + iBalances := interpreter.Balances{} |
| 23 | + for account, assetsRaw := range balances { |
| 24 | + if iBalances[account] == nil { |
| 25 | + iBalances[account] = interpreter.AccountBalance{} |
| 26 | + } |
| 27 | + |
| 28 | + assets, ok := assetsRaw.(map[string]any) |
| 29 | + if !ok { |
| 30 | + return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected nested object for account %v", account)) |
| 31 | + } |
| 32 | + |
| 33 | + for asset, amountRaw := range assets { |
| 34 | + amount, ok := amountRaw.(float64) |
| 35 | + if !ok { |
| 36 | + return interpreter.Balances{}, mcp.NewToolResultError(fmt.Sprintf("Expected float for amount: %v", amountRaw)) |
| 37 | + } |
| 38 | + |
| 39 | + n, _ := big.NewFloat(amount).Int(new(big.Int)) |
| 40 | + iBalances[account][asset] = n |
| 41 | + } |
| 42 | + } |
| 43 | + return iBalances, nil |
| 44 | +} |
| 45 | + |
| 46 | +func parseVarsJson(varsRaw any) (map[string]string, *mcp.CallToolResult) { |
| 47 | + vars, ok := varsRaw.(map[string]any) |
| 48 | + if !ok { |
| 49 | + return map[string]string{}, mcp.NewToolResultError(fmt.Sprintf("Expected an object as vars, got: <%#v>", varsRaw)) |
| 50 | + } |
| 51 | + |
| 52 | + iVars := map[string]string{} |
| 53 | + for key, rawValue := range vars { |
| 54 | + |
| 55 | + value, ok := rawValue.(string) |
| 56 | + if !ok { |
| 57 | + return map[string]string{}, mcp.NewToolResultError(fmt.Sprintf("Expected %s var to be a string, got: %T instead", key, rawValue)) |
| 58 | + } |
| 59 | + |
| 60 | + iVars[key] = value |
| 61 | + } |
| 62 | + |
| 63 | + return iVars, nil |
| 64 | +} |
| 65 | + |
| 66 | +func addEvalTool(s *server.MCPServer) { |
| 67 | + tool := mcp.NewTool("evaluate", |
| 68 | + mcp.WithDescription("Evaluate a numscript program"), |
| 69 | + mcp.WithIdempotentHintAnnotation(true), |
| 70 | + mcp.WithReadOnlyHintAnnotation(true), |
| 71 | + mcp.WithOpenWorldHintAnnotation(false), |
| 72 | + mcp.WithString("script", |
| 73 | + mcp.Required(), |
| 74 | + mcp.Description("The numscript source"), |
| 75 | + ), |
| 76 | + mcp.WithObject("balances", |
| 77 | + mcp.Required(), |
| 78 | + mcp.Description(`The accounts' balances. A nested map from the account name, to the asset, to its integer amount. |
| 79 | + For example: { "alice": { "USD/2": 100, "EUR/2": -42 }, "bob": { "BTC": 1 } } |
| 80 | + `), |
| 81 | + ), |
| 82 | + mcp.WithObject("vars", |
| 83 | + mcp.Required(), |
| 84 | + mcp.Description(`The stringified variables to be passed to the script's "vars" block. |
| 85 | + For example: { "acc": "alice", "mon": "EUR 100" } can be passed to the following script: |
| 86 | + vars { |
| 87 | + monetary $mon |
| 88 | + account $acc |
| 89 | + } |
| 90 | +
|
| 91 | + send $mon ( |
| 92 | + source = $acc |
| 93 | + destination = @world |
| 94 | + ) |
| 95 | + `), |
| 96 | + ), |
| 97 | + ) |
| 98 | + s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 99 | + script, err := request.RequireString("script") |
| 100 | + if err != nil { |
| 101 | + return mcp.NewToolResultError(err.Error()), nil |
| 102 | + } |
| 103 | + |
| 104 | + parsed := parser.Parse(script) |
| 105 | + if len(parsed.Errors) != 0 { |
| 106 | + out := make([]string, len(parsed.Errors)) |
| 107 | + for index, err := range parsed.Errors { |
| 108 | + out[index] = err.Msg |
| 109 | + } |
| 110 | + mcp.NewToolResultError(strings.Join(out, ", ")) |
| 111 | + } |
| 112 | + |
| 113 | + balances, mcpErr := parseBalancesJson(request.GetArguments()["balances"]) |
| 114 | + if mcpErr != nil { |
| 115 | + return mcpErr, nil |
| 116 | + } |
| 117 | + |
| 118 | + vars, mcpErr := parseVarsJson(request.GetArguments()["vars"]) |
| 119 | + if mcpErr != nil { |
| 120 | + return mcpErr, nil |
| 121 | + } |
| 122 | + |
| 123 | + out, iErr := interpreter.RunProgram( |
| 124 | + ctx, |
| 125 | + parsed.Value, |
| 126 | + vars, |
| 127 | + interpreter.StaticStore{ |
| 128 | + Balances: balances, |
| 129 | + }, |
| 130 | + map[string]struct{}{}, |
| 131 | + ) |
| 132 | + if iErr != nil { |
| 133 | + return mcp.NewToolResultError(iErr.Error()), nil |
| 134 | + } |
| 135 | + return mcp.NewToolResultJSON(*out) |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +func addCheckTool(s *server.MCPServer) { |
| 140 | + tool := mcp.NewTool("check", |
| 141 | + mcp.WithDescription("Check a program for parsing error or static analysis errors"), |
| 142 | + mcp.WithIdempotentHintAnnotation(true), |
| 143 | + mcp.WithReadOnlyHintAnnotation(true), |
| 144 | + mcp.WithOpenWorldHintAnnotation(false), |
| 145 | + mcp.WithString("script", |
| 146 | + mcp.Required(), |
| 147 | + mcp.Description("The numscript source"), |
| 148 | + ), |
| 149 | + ) |
| 150 | + |
| 151 | + s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 152 | + script, err := request.RequireString("script") |
| 153 | + if err != nil { |
| 154 | + return mcp.NewToolResultError(err.Error()), nil |
| 155 | + } |
| 156 | + |
| 157 | + checkResult := analysis.CheckSource(script) |
| 158 | + |
| 159 | + var errors []any |
| 160 | + for _, d := range checkResult.Diagnostics { |
| 161 | + errors = append(errors, map[string]any{ |
| 162 | + "kind": d.Kind.Message(), |
| 163 | + "severity": analysis.SeverityToString(d.Kind.Severity()), |
| 164 | + "span": d.Range, |
| 165 | + }) |
| 166 | + } |
| 167 | + |
| 168 | + return mcp.NewToolResultJSON(map[string]any{ |
| 169 | + "errors": errors, |
| 170 | + }) |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | +func RunServer() error { |
| 175 | + // Create a new MCP server |
| 176 | + s := server.NewMCPServer( |
| 177 | + "Numscript", |
| 178 | + "0.0.1", |
| 179 | + server.WithToolCapabilities(false), |
| 180 | + server.WithRecovery(), |
| 181 | + server.WithInstructions(` |
| 182 | + 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. |
| 183 | + `), |
| 184 | + ) |
| 185 | + addEvalTool(s) |
| 186 | + addCheckTool(s) |
| 187 | + |
| 188 | + // Start the server |
| 189 | + if err := server.ServeStdio(s); err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + return nil |
| 194 | +} |
0 commit comments