get-portfolio-transactions
Retrieve cryptocurrency portfolio transaction history to track buys, sells, and transfers. Filter by coin, currency, and date for detailed financial analysis.
Instructions
Get a list of portfolio transactions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| shareToken | No | Portfolio share token. You can get your share token from the portfolio you want to retrive data from by clicking Share button on CoinStats web app portfolio tracker section - top right. | |
| page | No | Page number | |
| limit | No | Number of results per page | |
| currency | Yes | Currency for price data | |
| coinId | No | Filter by coin ID |
Implementation Reference
- src/services/request.ts:35-97 (handler)Core handler function that processes tool parameters, constructs the API URL for /portfolio/transactions, makes the HTTP GET request using makeRequestCsApi, and returns the JSON response formatted for MCP.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/services/request.ts:4-32 (helper)Low-level HTTP request maker using fetch to CoinStats API with API key authentication and query parameter handling.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; } }
- src/tools/toolConfigs.ts:385-403 (schema)Tool configuration defining the name 'get-portfolio-transactions', description, endpoint '/portfolio/transactions', method 'GET', and Zod schemas for input parameters including shareToken, page, limit, currency, coinId.// Portfolio Transactions Tool Configuration { name: 'get-portfolio-transactions', description: 'Get a list of portfolio transactions.', endpoint: '/portfolio/transactions', method: 'GET', parameters: { shareToken: z .string() .optional() .describe( 'Portfolio share token. You can get your share token from the portfolio you want to retrive data from by clicking Share button on CoinStats web app portfolio tracker section - top right.' ), page: z.number().optional().describe('Page number').default(1), limit: z.number().optional().describe('Number of results per page').default(20), currency: z.string().describe('Currency for price data'), coinId: z.string().optional().describe('Filter by coin ID'), }, },
- src/tools/toolFactory.ts:19-79 (registration)Registers the 'get-portfolio-transactions' tool (and all others) with the MCP server using server.tool(), providing a generic async handler that delegates to universalApiHandler for 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/index.ts:1-30 (registration)Main MCP server setup that imports and calls registerTools with allToolConfigs, thereby registering the get-portfolio-transactions tool.#!/usr/bin/env node import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { registerTools } from './tools/toolFactory.js'; import { allToolConfigs } from './tools/toolConfigs.js'; // Create server instance const server = new McpServer({ name: 'coinstats-mcp', version: '1.0.0', capabilities: { resources: {}, tools: {}, }, }); // Register all tools from configurations registerTools(server, allToolConfigs); async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error('CoinStats MCP Server running on stdio'); } main().catch((error) => { console.error('Fatal error in main():', error); process.exit(1); });