read_go_playground_url
Extract Go code from a Go Playground URL to access and reuse existing snippets without manual copying.
Instructions
Read Go code from an existing Go Playground URL
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | The Go Playground URL to read code from (e.g., https://go.dev/play/abc123 or https://play.golang.org/p/abc123) |
Implementation Reference
- src/go-playground-client.ts:252-316 (handler)Core implementation of readGoPlaygroundUrl: validates URL, extracts share ID, fetches Go source code from playground API endpoint, returns success with code or failure with error.export const readGoPlaygroundUrl = (config: Partial<GoPlaygroundConfig> = {}) => async (url: string): Promise<{ success: true; code: string } | { success: false; error: string }> => { // Validate URL if (!isGoPlaygroundUrl(url)) { return { success: false, error: 'Invalid Go Playground URL. Expected format: https://go.dev/play/xyz or https://play.golang.org/p/xyz', }; } const finalConfig = { ...createDefaultConfig(), ...config }; const client = createHttpClient(finalConfig); try { // Extract share ID from URL let shareId: string | null = null; if (url.includes('go.dev/play/')) { // Handles both go.dev/play/<id> and go.dev/play/p/<id> const after = url.split('go.dev/play/')[1]; const parts = after.split('/').filter(Boolean); if (parts.length === 1) { shareId = parts[0]; } else if (parts.length >= 2 && parts[0] === 'p') { shareId = parts[1]; } } else if (url.includes('play.golang.org/p/')) { shareId = url.split('play.golang.org/p/')[1].split('/')[0]; } if (!shareId) { return { success: false, error: 'Invalid Go Playground URL format', }; } // Fetch the code from the playground const response = await client.get(`/p/${shareId}.go`); if (response.status !== 200) { return { success: false, error: `Failed to fetch code: HTTP ${response.status}`, }; } return { success: true, code: response.data, }; } catch (error) { console.error('Failed to read playground URL:', error); if (isAxiosError(error)) { return { success: false, error: `Network error: ${error.message}`, }; } return { success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } };
- src/mcp-server.ts:262-280 (handler)MCP server handler factory for 'read_go_playground_url' tool: destructures args, calls client.readUrl, formats markdown response with code block or error.const createReadGoPlaygroundUrlHandler = (client: ReturnType<typeof createGoPlaygroundClient>) => async (args: ReadGoPlaygroundUrlArgs): Promise<MCPToolResponse> => { try { const { url } = args; const result = await client.readUrl(url); if (!result.success) { const responseText = `❌ Failed to read code from playground URL.\n\n**Error:** ${result.error}`; return createSuccessResponse(responseText); } const responseText = `✅ Successfully read code from playground URL!\n\n**Code:**\n\`\`\`go\n${result.code}\n\`\`\``; return createSuccessResponse(responseText); } catch (error) { console.error('Error in handleReadGoPlaygroundUrl:', error); return createErrorResponse(error); } };
- src/mcp-server.ts:300-306 (registration)Registration of all tool handlers in the router, including createReadGoPlaygroundUrlHandler(client) for TOOL_NAMES.READ_GO_PLAYGROUND_URL.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;
- src/mcp-server.ts:103-116 (schema)Tool definition for list_tools including name 'read_go_playground_url' and input schema validation.{ name: TOOL_NAMES.READ_GO_PLAYGROUND_URL, description: 'Read Go code from an existing Go Playground URL', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The Go Playground URL to read code from (e.g., https://go.dev/play/abc123 or https://play.golang.org/p/abc123)', }, }, required: ['url'], }, },
- src/types.ts:91-93 (schema)TypeScript type definition for tool input arguments.export type ReadGoPlaygroundUrlArgs = { readonly url: GoPlaygroundUrl; };
- src/types.ts:211-218 (helper)Runtime type guard/validator for ReadGoPlaygroundUrlArgs, checks object structure and URL validity.export const isReadGoPlaygroundUrlArgs = (args: unknown): args is ReadGoPlaygroundUrlArgs => { return ( typeof args === 'object' && args !== null && 'url' in args && isGoPlaygroundUrl((args as Record<string, unknown>).url) ); };