run_go_code
Execute Go code in the Go Playground sandbox and return results, with optional go vet analysis for code quality.
Instructions
Run Go code in the Go Playground and return execution results
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The Go code to execute | |
| withVet | No | Whether to run go vet on the code (default: true) |
Implementation Reference
- src/mcp-server.ts:216-228 (handler)MCP tool handler for 'run_go_code'. Creates a handler that destructures args (code, withVet), calls client.runCode(), formats the response, and handles errors.
const createRunGoCodeHandler = (client: ReturnType<typeof createGoPlaygroundClient>) => async (args: RunGoCodeArgs): Promise<MCPToolResponse> => { try { const { code, withVet = true } = args; const result = await client.runCode(code, withVet); const responseText = formatRunResponse(result); return createSuccessResponse(responseText); } catch (error) { console.error('Error in handleRunGoCode:', error); return createErrorResponse(error); } }; - src/go-playground-client.ts:113-159 (handler)Core execution function for running Go code. Validates input via validateGoCode, constructs a request to the Go Playground /compile API, processes the response via processRunResponse, and returns a typed result.
export const runGoCode = (config: Partial<GoPlaygroundConfig> = {}) => async ( code: string, withVet: boolean = true ): Promise<MCPGoPlaygroundResult> => { // Validate input const validationResult = validateGoCode(code); if (!validationResult.success) { return { success: false, errors: validationResult.error.message, }; } const finalConfig = { ...createDefaultConfig(), ...config }; const client = createHttpClient(finalConfig); try { const request: GoPlaygroundRunRequest = { version: 2, body: validationResult.data, withVet, }; const response = await client.post<GoPlaygroundResponse>( '/compile', request, { headers: { 'Content-Type': 'application/json', }, } ); // Debug logging console.error( 'Go Playground API Response:', JSON.stringify(response.data, null, 2) ); return processRunResponse(response.data); } catch (error) { console.error('Go Playground API Error:', error); return handleApiError(error); } }; - src/go-playground-client.ts:70-110 (handler)Helper function that processes the Go Playground API response, checking for compilation errors, runtime errors (stderr), and extracting stdout output.
const processRunResponse = ( result: GoPlaygroundResponse ): MCPGoPlaygroundResult => { // Validate response structure if (!result || typeof result !== 'object') { return { success: false, errors: 'Invalid response from Go Playground API', }; } // Check for compilation errors (Errors field contains compilation errors) if (result.Errors && result.Errors.trim() !== '') { return { success: false, errors: result.Errors, exitCode: result.Status, }; } // Check for runtime errors (runtime errors appear in Events with Kind: 'stderr') const stderrEvents = result.Events?.filter(event => event.Kind === 'stderr') ?? []; if (stderrEvents.length > 0) { const errorMessages = stderrEvents.map(event => event.Message).join(''); return { success: false, errors: errorMessages, exitCode: result.Status, }; } // Process output using functional approach const processedOutput = processEvents(result.Events ?? []); return { success: true, output: processedOutput.output, exitCode: result.Status, }; }; - src/types.ts:70-73 (schema)Type definition for run_go_code arguments: code (branded GoCode string) and optional withVet boolean.
export type RunGoCodeArgs = { readonly code: GoCode; readonly withVet?: boolean; }; - src/mcp-server.ts:47-68 (registration)Tool definition/schema registration for 'run_go_code' with name, description, and inputSchema (code as required string, withVet as optional boolean).
const createToolDefinitions = (): readonly Tool[] => [ { name: TOOL_NAMES.RUN_GO_CODE, description: 'Run Go code in the Go Playground and return execution results', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'The Go code to execute', }, withVet: { type: 'boolean', description: 'Whether to run go vet on the code (default: true)', default: true, }, }, required: ['code'], }, },