Skip to main content
Glama
samber

Go Playground MCP Server

by samber

share_go_code

Generate shareable URLs for Go code snippets to facilitate collaboration and code sharing in development workflows.

Instructions

Share Go code and get a shareable URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesThe Go code to share

Implementation Reference

  • Core handler function that validates the Go code, sends a POST request to the Go Playground '/share' endpoint with the code, extracts the share ID from the response, and constructs the shareable frontend URL (https://go.dev/play/p/{shareId}). Returns the URL or null on failure.
    export const shareGoCode =
      (config: Partial<GoPlaygroundConfig> = {}) =>
      async (code: string): Promise<ShareUrl | null> => {
        // Validate input
        const validationResult = validateGoCode(code);
        if (!validationResult.success) {
          console.error(
            'Invalid Go code for sharing:',
            validationResult.error.message
          );
          return null;
        }
    
        const finalConfig = { ...createDefaultConfig(), ...config };
        const client = createHttpClient(finalConfig);
    
        try {
          const request: GoPlaygroundShareRequest = {
            body: validationResult.data,
          };
    
          const response = await client.post<GoPlaygroundShareResponse>(
            '/share',
            request.body,
            {
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
              },
            }
          );
    
          const shareId = response.data;
          // Use the frontend URL for sharing, not the API URL
          return createShareUrl(`${finalConfig.frontendUrl}/p/${shareId}`);
        } catch (error) {
          console.error('Failed to share code:', error);
          return null;
        }
      };
  • Registers the 'share_go_code' tool in the MCP server with its name, description, and input schema (object with required 'code' string). This is returned by listTools.
    {
      name: TOOL_NAMES.SHARE_GO_CODE,
      description: 'Share Go code and get a shareable URL',
      inputSchema: {
        type: 'object',
        properties: {
          code: {
            type: 'string',
            description: 'The Go code to share',
          },
        },
        required: ['code'],
      },
    },
  • MCP-specific tool handler that extracts 'code' from args, calls the client's shareCode method, formats a success/error response with the share URL, and returns it as MCPToolResponse.
    const createShareGoCodeHandler =
      (client: ReturnType<typeof createGoPlaygroundClient>) =>
      async (args: ShareGoCodeArgs): Promise<MCPToolResponse> => {
        try {
          const { code } = args;
          const shareUrl = await client.shareCode(code);
    
          const responseText = shareUrl
            ? `✅ Code shared successfully!\n\n**Share URL:** ${shareUrl}`
            : '❌ Failed to share code. Please try again.';
    
          return createSuccessResponse(responseText);
        } catch (error) {
          console.error('Error in handleShareGoCode:', error);
          return createErrorResponse(error);
        }
      };
  • TypeScript type definition for the tool's input arguments: an object with a required 'code' field of type GoCode (branded string).
    export type ShareGoCodeArgs = {
      readonly code: GoCode;
    };
  • Runtime type guard/validator for ShareGoCodeArgs, checks if args is an object with non-null 'code' that is a valid GoCode (non-empty string). Used in the tool router for input validation.
    export const isShareGoCodeArgs = (args: unknown): args is ShareGoCodeArgs => {
      return (
        typeof args === 'object' &&
        args !== null &&
        'code' in args &&
        isGoCode((args as Record<string, unknown>).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