Skip to main content
Glama

run_go_code

Execute Go code in a sandboxed environment to test and verify program output. This tool runs Go programs and returns execution results for debugging or demonstration purposes.

Instructions

Run Go code in the Go Playground and return execution results

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesThe Go code to execute
withVetNoWhether to run go vet on the code (default: true)

Implementation Reference

  • The MCP server handler function for the 'run_go_code' tool. It extracts arguments, calls the Go playground client's runCode method, formats the result, and returns an MCPToolResponse.
    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); } };
  • Core helper function that performs the actual API call to the Go Playground '/compile' endpoint, validates input, handles errors, and processes the response into MCPGoPlaygroundResult.
    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); } };
  • Registers the run_go_code handler in the tools router's handlers map, associating TOOL_NAMES.RUN_GO_CODE with the created handler function.
    const handlers = { [TOOL_NAMES.RUN_GO_CODE]: createRunGoCodeHandler(client), [TOOL_NAMES.SHARE_GO_CODE]: createShareGoCodeHandler(client), [TOOL_NAMES.RUN_AND_SHARE_GO_CODE]: createRunAndShareGoCodeHandler(client), [TOOL_NAMES.READ_GO_PLAYGROUND_URL]: createReadGoPlaygroundUrlHandler(client), [TOOL_NAMES.EXECUTE_GO_PLAYGROUND_URL]: createExecuteGoPlaygroundUrlHandler(client), } as const;
  • TypeScript type definition for the input arguments of the run_go_code tool.
    export type RunGoCodeArgs = { readonly code: GoCode; readonly withVet?: boolean; };
  • JSON schema definition for the run_go_code tool, used in listTools response for input validation.
    { 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'], },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/samber/go-playground-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server