GenerateMCPScriptPythonToolScript generates the Python script for a mcp-script tool Python scripts receive inputs as a dictionary (parsed from JSON stdin): - Input parameters are available as a pre-parsed 'inputs' dictionary - Individual parameters can be destructured: param = inputs.get('param', de
(toolConfig *MCPScriptToolConfig)
| 256 | // - Outputs are printed to stdout as JSON |
| 257 | // - Environment variables from env: field are available via os.environ |
| 258 | func GenerateMCPScriptPythonToolScript(toolConfig *MCPScriptToolConfig) string { |
| 259 | mcpScriptsLog.Printf("Generating Python tool script: tool=%s, input_count=%d", toolConfig.Name, len(toolConfig.Inputs)) |
| 260 | var sb strings.Builder |
| 261 | |
| 262 | sb.WriteString("#!/usr/bin/env python3\n") |
| 263 | sb.WriteString("# Auto-generated mcp-script tool: " + toolConfig.Name + "\n") |
| 264 | sb.WriteString(formatMultiLineComment(toolConfig.Description, "# ") + "\n") |
| 265 | sb.WriteString("import json\n") |
| 266 | sb.WriteString("import os\n") |
| 267 | sb.WriteString("import sys\n\n") |
| 268 | |
| 269 | // Add wrapper code to read inputs from stdin |
| 270 | sb.WriteString("# Read inputs from stdin (JSON format)\n") |
| 271 | sb.WriteString("try:\n") |
| 272 | sb.WriteString(" inputs = json.loads(sys.stdin.read()) if not sys.stdin.isatty() else {}\n") |
| 273 | sb.WriteString("except (json.JSONDecodeError, Exception):\n") |
| 274 | sb.WriteString(" inputs = {}\n\n") |
| 275 | |
| 276 | // Add helper comment about input parameters |
| 277 | if len(toolConfig.Inputs) > 0 { |
| 278 | sb.WriteString("# Input parameters available in 'inputs' dictionary:\n") |
| 279 | // Sort input names for stable code generation |
| 280 | inputNames := sliceutil.SortedKeys(toolConfig.Inputs) |
| 281 | for _, paramName := range inputNames { |
| 282 | param := toolConfig.Inputs[paramName] |
| 283 | defaultValue := "" |
| 284 | if param.Default != nil { |
| 285 | defaultValue = fmt.Sprintf(", default=%v", param.Default) |
| 286 | } |
| 287 | fmt.Fprintf(&sb, "# %s = inputs.get('%s'%s) # %s\n", |
| 288 | stringutil.SanitizePythonVariableName(paramName), paramName, defaultValue, param.Description) |
| 289 | } |
| 290 | sb.WriteString("\n") |
| 291 | } |
| 292 | |
| 293 | // Add user's Python code |
| 294 | sb.WriteString("# User code:\n") |
| 295 | sb.WriteString(toolConfig.Py + "\n") |
| 296 | |
| 297 | return sb.String() |
| 298 | } |
| 299 | |
| 300 | // GenerateMCPScriptGoToolScript generates the Go script for a mcp-script tool |
| 301 | // Go scripts receive inputs as JSON via stdin and output JSON to stdout: |