create_link
Create short links on Dub.co by specifying destination URLs, custom slugs, and domain preferences for URL shortening.
Instructions
Create a new short link on dub.co, asking the user which domain to use
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | The destination URL to shorten | |
| key | No | Optional custom slug for the short link. If not provided, a random slug will be generated. | |
| externalId | No | Optional external ID for the link | |
| domain | No | Optional domain slug to use. If not provided, the primary domain will be used. |
Implementation Reference
- src/index.ts:276-361 (handler)The handler function that implements the create_link tool logic: validates input, selects domain (user-specified or primary), constructs params, calls Dub.co API POST /links, handles errors, and returns the created short link details.private async createLink(args: any): Promise<any> { if (!args.url) { throw new McpError( ErrorCode.InvalidParams, 'URL is required' ); } try { // Determine which domain to use let domain: Domain; if (args.domain) { // If domain is specified, try to find it const foundDomain = await this.getDomainBySlug(args.domain); if (!foundDomain) { return { content: [ { type: 'text', text: `Domain "${args.domain}" not found. Using primary domain instead.`, }, ], isError: false, }; } domain = foundDomain; } else { // Otherwise use the primary domain domain = await this.getPrimaryDomain(); } // Create the link with the selected domain const createParams: CreateLinkParams = { url: args.url, domain: domain.slug, }; if (args.key) { createParams.key = args.key; } if (args.externalId) { createParams.externalId = args.externalId; } const response = await this.axiosInstance.post('/links', createParams); const link = response.data; return { content: [ { type: 'text', text: `Short link created: ${link.shortLink}\n\nDestination: ${link.url}\nID: ${link.id}`, }, ], }; } catch (error) { if (axios.isAxiosError(error)) { const axiosError = error as AxiosError<ApiErrorResponse>; const statusCode = axiosError.response?.status; const errorData = axiosError.response?.data; const errorMessage = errorData?.error || errorData?.message || axiosError.message; return { content: [ { type: 'text', text: `Error creating link: ${statusCode} - ${errorMessage}`, }, ], isError: true, }; } return { content: [ { type: 'text', text: `Error creating link: ${(error as Error).message}`, }, ], isError: true, }; } }
- src/index.ts:104-129 (registration)Tool registration in ListToolsRequestHandler, defining name, description, and inputSchema for create_link.{ name: 'create_link', description: 'Create a new short link on dub.co, asking the user which domain to use', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The destination URL to shorten', }, key: { type: 'string', description: 'Optional custom slug for the short link. If not provided, a random slug will be generated.', }, externalId: { type: 'string', description: 'Optional external ID for the link', }, domain: { type: 'string', description: 'Optional domain slug to use. If not provided, the primary domain will be used.' } }, required: ['url'], }, },
- src/index.ts:45-51 (schema)TypeScript interface defining the parameters for creating a link, used in the handler.interface CreateLinkParams { url: string; domain?: string; key?: string; externalId?: string; // ... other optional parameters }
- src/index.ts:202-203 (handler)Dispatch case in CallToolRequestHandler that routes create_link calls to the createLink method.case 'create_link': return await this.createLink(request.params.arguments);
- src/index.ts:256-269 (helper)Helper method to retrieve the primary domain, used by createLink to select default domain.private async getPrimaryDomain(): Promise<Domain> { const domains = await this.getDomains(); if (domains.length === 0) { throw new McpError( ErrorCode.InvalidRequest, 'No domains available in your workspace' ); } // Find the primary domain or use the first one const primaryDomain = domains.find(domain => domain.primary) || domains[0]; return primaryDomain; }