get-coin-by-id
Retrieve detailed cryptocurrency data including price, market cap, and volume for a specific coin using its unique identifier.
Instructions
Get detailed information about a specific cryptocurrency based on its unique identifier.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| coinId | Yes | The identifier of coin, which you received from /coins call response. | |
| currency | No | Currency for price data | USD |
Input Schema (JSON Schema)
{
"properties": {
"coinId": {
"description": "The identifier of coin, which you received from /coins call response.",
"type": "string"
},
"currency": {
"default": "USD",
"description": "Currency for price data",
"type": "string"
}
},
"required": [
"coinId"
],
"type": "object"
}
Implementation Reference
- src/tools/toolConfigs.ts:80-89 (schema)Tool configuration defining name, description, endpoint '/coins/{coinId}', method 'GET', and Zod input schema for parameters coinId (required) and currency (optional, default 'USD').{ name: 'get-coin-by-id', description: 'Get detailed information about a specific cryptocurrency based on its unique identifier.', endpoint: '/coins/{coinId}', method: 'GET', parameters: { coinId: z.string().describe('The identifier of coin, which you received from /coins call response.'), currency: z.string().optional().describe('Currency for price data').default('USD'), }, },
- src/tools/toolFactory.ts:19-79 (registration)Registers all tools from toolConfigs, including 'get-coin-by-id', by calling server.tool() with shared handler logic that delegates to universalApiHandler for non-local API calls.export function registerTools(server: McpServer, toolConfigs: ToolConfig<any>[]) { toolConfigs.forEach((config) => { server.tool(config.name, config.description, config.parameters, async (params: Record<string, any>) => { // Handle local operations if (config.isLocal) { // Handle specific local tools if (config.name === 'save-share-token') { await saveToCache('shareToken', params.shareToken); return { content: [ { type: 'text', text: 'Share token saved successfully', }, ], }; } if (config.name === 'get-share-token') { const shareToken = await getFromCache('shareToken'); return { content: [ { type: 'text', text: shareToken ? shareToken : 'No share token found in cache', isError: !shareToken, }, ], }; } // Future local tools can be added here // Default response for unhandled local tools return { content: [ { type: 'text', text: 'Operation completed', }, ], }; } // Handle API operations const basePath = config.basePath || COINSTATS_API_BASE; const method = config.method || 'GET'; // Methods that typically have a request body const bodyMethods = ['POST', 'PUT', 'PATCH', 'DELETE']; // For GET/DELETE requests, all params go in the URL // For POST/PUT/PATCH, send params as the body if (bodyMethods.includes(method.toUpperCase())) { return universalApiHandler(basePath, config.endpoint, method, {}, params); } else { return universalApiHandler(basePath, config.endpoint, method, params); } }); }); }
- src/services/request.ts:35-97 (handler)Core execution logic for API-based tools like get-coin-by-id: interpolates path params (e.g. {coinId} → actual value), handles query params, calls makeRequestCsApi to fetch data, and formats response as MCP content.export async function universalApiHandler<T>( basePath: string, endpoint: string, method: string = 'GET', params: Record<string, any> = {}, body?: any ): Promise<{ content: Array<{ type: 'text'; text: string; isError?: boolean }>; }> { try { // Handle path parameters - replace {paramName} in endpoint with actual values let processedEndpoint = endpoint; let processedParams = { ...params }; // Find all path parameters in the endpoint (e.g., {coinId}, {id}, {type}) const pathParamMatches = endpoint.match(/\{([^}]+)\}/g); if (pathParamMatches) { for (const match of pathParamMatches) { const paramName = match.slice(1, -1); // Remove { and } if (processedParams[paramName] !== undefined) { // Replace the placeholder with the actual value processedEndpoint = processedEndpoint.replace(match, processedParams[paramName]); // Remove the parameter from query params since it's now part of the path delete processedParams[paramName]; } else { throw new Error(`Required path parameter '${paramName}' is missing`); } } } // MCP clients might not support '~' in parameter names, so we replace '-' with '~' specifically for the /coins endpoint before making the request. if (endpoint === '/coins') { processedParams = Object.entries(processedParams).reduce((acc, [key, value]) => { acc[key.replace(/-/g, '~')] = value; return acc; }, {} as Record<string, any>); } const url = `${basePath}${processedEndpoint}`; const data = await makeRequestCsApi<T>(url, method, processedParams, body); if (!data) { return { content: [{ type: 'text', text: 'Something went wrong', isError: true }], }; } return { content: [ { type: 'text', text: JSON.stringify(data), }, ], }; } catch (error) { return { content: [{ type: 'text', text: `Error: ${error}`, isError: true }], }; } }
- src/index.ts:17-18 (registration)Top-level call to registerTools(server, allToolConfigs), which registers the get-coin-by-id tool with the MCP server.// Register all tools from configurations registerTools(server, allToolConfigs);
- src/services/request.ts:4-32 (helper)Low-level HTTP request function that performs the actual fetch to CoinStats API with API key authentication, used by universalApiHandler.export async function makeRequestCsApi<T>(url: string, method: string = 'GET', params: Record<string, any> = {}, body?: any): Promise<T | null> { const headers = { 'X-API-KEY': COINSTATS_API_KEY, 'Content-Type': 'application/json', }; try { // Build request options const options: RequestInit = { method, headers }; // Add body for non-GET requests if provided if (method !== 'GET' && body) { options.body = JSON.stringify(body); } // Add query params for all requests const queryParams = new URLSearchParams(params); const queryString = queryParams.toString(); const urlWithParams = queryString ? `${url}?${queryString}` : url; const response = await fetch(urlWithParams, options); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return (await response.json()) as T; } catch (error) { return null; } }