wolfram_query_with_assumptions
Clarify ambiguous Wolfram Alpha queries by specifying assumptions when initial results show multiple interpretations, ensuring accurate computational answers.
Instructions
Query Wolfram Alpha with specific assumptions when the initial query returns multiple interpretations. Use this when you need to clarify ambiguous queries.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | The exact same input from the previous query | |
| assumption | Yes | The assumption value to use from the previous query result | |
| maxchars | No | Maximum number of characters in the response (default: 6800) |
Implementation Reference
- src/index.ts:186-331 (handler)Core handler function that executes the tool logic for 'wolfram_query_with_assumptions' by calling the Wolfram Alpha API with the 'assumption' parameter and handling the response, including parsing assumptions for metadata.private async handleWolframQuery(params: WolframQueryParams) { if (!this.apiKey) { throw new McpError( ErrorCode.InvalidRequest, 'Wolfram Alpha App ID not configured. Please set the WOLFRAM_ALPHA_APP_ID environment variable.' ); } if (!params.input || params.input.trim().length === 0) { throw new McpError( ErrorCode.InvalidParams, 'Input parameter is required and cannot be empty' ); } try { const url = 'https://www.wolframalpha.com/api/v1/llm-api'; const queryParams: Record<string, string | number> = { appid: this.apiKey, input: params.input.trim(), }; // Add optional parameters if provided if (params.maxchars) queryParams.maxchars = params.maxchars; if (params.assumption) queryParams.assumption = params.assumption; if (params.units) queryParams.units = params.units; if (params.currency) queryParams.currency = params.currency; if (params.countrycode) queryParams.countrycode = params.countrycode; if (params.languagecode) queryParams.languagecode = params.languagecode; if (params.location) queryParams.location = params.location; if (params.timezone) queryParams.timezone = params.timezone; if (params.width) queryParams.width = params.width; if (params.maxwidth) queryParams.maxwidth = params.maxwidth; if (params.plotwidth) queryParams.plotwidth = params.plotwidth; if (params.scantimeout) queryParams.scantimeout = params.scantimeout; if (params.formattimeout) queryParams.formattimeout = params.formattimeout; if (params.parsetimeout) queryParams.parsetimeout = params.parsetimeout; if (params.totaltimeout) queryParams.totaltimeout = params.totaltimeout; const response = await axios.get(url, { params: queryParams, timeout: (params.totaltimeout || 30) * 1000, // Convert to milliseconds headers: { 'User-Agent': 'WolframAlpha-MCP-Server/1.0.0', }, }); if (response.status === 200) { const result = response.data; // Parse the response to extract useful information const lines = result.split('\n'); let formattedResult = result; // Look for assumptions in the response const assumptions: string[] = []; let inAssumptions = false; for (const line of lines) { if (line.includes('Assumptions:') || line.includes('Input interpretation:')) { inAssumptions = true; } else if (inAssumptions && line.trim() && !line.includes('Result:')) { if (line.includes('|')) { assumptions.push(line.trim()); } } else if (line.includes('Result:')) { inAssumptions = false; } } let metadata = `**Query:** "${params.input}"\n\n`; if (assumptions.length > 0) { metadata += `**Available Assumptions:**\n${assumptions.join('\n')}\n\n`; metadata += `*If the result is not what you expected, you can use the wolfram_query_with_assumptions tool with one of the assumption values above.*\n\n`; } return { content: [ { type: 'text', text: metadata + formattedResult, }, ], }; } else { throw new McpError( ErrorCode.InternalError, `Wolfram Alpha API returned status ${response.status}: ${response.statusText}` ); } } catch (error) { if (axios.isAxiosError(error)) { if (error.response?.status === 501) { const errorMessage = error.response.data || 'Input cannot be interpreted by Wolfram Alpha'; throw new McpError( ErrorCode.InvalidParams, `Wolfram Alpha could not interpret the input: "${params.input}". ${errorMessage}. Try rephrasing your query with simpler, more specific terms.` ); } else if (error.response?.status === 400) { throw new McpError( ErrorCode.InvalidParams, 'Invalid request parameters. Please check your input and try again.' ); } else if (error.response?.status === 403) { const errorData = error.response.data || ''; if (errorData.includes('Invalid appid')) { throw new McpError( ErrorCode.InvalidRequest, 'Invalid Wolfram Alpha App ID. Please check your WOLFRAM_ALPHA_APP_ID environment variable.' ); } else if (errorData.includes('Appid missing')) { throw new McpError( ErrorCode.InvalidRequest, 'Wolfram Alpha App ID is missing. Please set the WOLFRAM_ALPHA_APP_ID environment variable.' ); } else { throw new McpError( ErrorCode.InvalidRequest, 'Authentication failed with Wolfram Alpha API.' ); } } else if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') { throw new McpError( ErrorCode.InternalError, 'Unable to connect to Wolfram Alpha API. Please check your internet connection.' ); } else if (error.code === 'ETIMEDOUT') { throw new McpError( ErrorCode.InternalError, 'Request to Wolfram Alpha API timed out. The query may be too complex or the service may be temporarily unavailable.' ); } else { throw new McpError( ErrorCode.InternalError, `Network error: ${error.message}` ); } } else { throw new McpError( ErrorCode.InternalError, `Unexpected error: ${error instanceof Error ? error.message : 'Unknown error'}` ); } } }
- src/index.ts:132-154 (registration)Registration of the 'wolfram_query_with_assumptions' tool in the ListTools response, including name, description, and input schema.{ name: 'wolfram_query_with_assumptions', description: 'Query Wolfram Alpha with specific assumptions when the initial query returns multiple interpretations. Use this when you need to clarify ambiguous queries.', inputSchema: { type: 'object', properties: { input: { type: 'string', description: 'The exact same input from the previous query', }, assumption: { type: 'string', description: 'The assumption value to use from the previous query result', }, maxchars: { type: 'number', description: 'Maximum number of characters in the response (default: 6800)', default: 6800, }, }, required: ['input', 'assumption'], }, },
- src/index.ts:135-153 (schema)JSON schema defining the input parameters for the 'wolfram_query_with_assumptions' tool.inputSchema: { type: 'object', properties: { input: { type: 'string', description: 'The exact same input from the previous query', }, assumption: { type: 'string', description: 'The assumption value to use from the previous query result', }, maxchars: { type: 'number', description: 'Maximum number of characters in the response (default: 6800)', default: 6800, }, }, required: ['input', 'assumption'], },
- src/index.ts:13-30 (schema)TypeScript interface defining input parameters used by the handler for both Wolfram query tools.interface WolframQueryParams { input: string; maxchars?: number; assumption?: string; units?: string; currency?: string; countrycode?: string; languagecode?: string; location?: string; timezone?: string; width?: number; maxwidth?: number; plotwidth?: number; scantimeout?: number; formattimeout?: number; parsetimeout?: number; totaltimeout?: number; }
- src/index.ts:241-254 (helper)Helper logic to parse available assumptions from the API response, used to generate metadata suggesting the use of 'wolfram_query_with_assumptions'.const assumptions: string[] = []; let inAssumptions = false; for (const line of lines) { if (line.includes('Assumptions:') || line.includes('Input interpretation:')) { inAssumptions = true; } else if (inAssumptions && line.trim() && !line.includes('Result:')) { if (line.includes('|')) { assumptions.push(line.trim()); } } else if (line.includes('Result:')) { inAssumptions = false; } }