search_compounds
Search the PubChem database for chemical compounds by name, CAS number, formula, or identifier to retrieve detailed molecular information and data.
Instructions
Search PubChem database for compounds by name, CAS number, formula, or identifier
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_records | No | Maximum number of results (1-10000, default: 100) | |
| query | Yes | Search query (compound name, CAS, formula, or identifier) | |
| search_type | No | Type of search to perform (default: name) |
Implementation Reference
- src/index.ts:832-880 (handler)Main handler function for the 'search_compounds' tool. Validates input arguments using isValidCompoundSearchArgs, performs PubChem API search for CIDs based on query and search_type, fetches additional properties for top 10 results, and returns formatted JSON response.private async handleSearchCompounds(args: any) { if (!isValidCompoundSearchArgs(args)) { throw new McpError(ErrorCode.InvalidParams, 'Invalid compound search arguments'); } try { const searchType = args.search_type || 'name'; const maxRecords = args.max_records || 100; const response = await this.apiClient.get(`/compound/${searchType}/${encodeURIComponent(args.query)}/cids/JSON`, { params: { MaxRecords: maxRecords, }, }); if (response.data?.IdentifierList?.CID?.length > 0) { const cids = response.data.IdentifierList.CID.slice(0, 10); const detailsResponse = await this.apiClient.get(`/compound/cid/${cids.join(',')}/property/MolecularFormula,MolecularWeight,CanonicalSMILES,IUPACName/JSON`); return { content: [ { type: 'text', text: JSON.stringify({ query: args.query, search_type: searchType, total_found: response.data.IdentifierList.CID.length, details: detailsResponse.data, }, null, 2), }, ], }; } return { content: [ { type: 'text', text: JSON.stringify({ message: 'No compounds found', query: args.query }, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to search compounds: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:371-382 (registration)Tool registration in the ListToolsRequestSchema handler, defining the name, description, and input schema for 'search_compounds'.name: 'search_compounds', description: 'Search PubChem database for compounds by name, CAS number, formula, or identifier', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query (compound name, CAS, formula, or identifier)' }, search_type: { type: 'string', enum: ['name', 'smiles', 'inchi', 'sdf', 'cid', 'formula'], description: 'Type of search to perform (default: name)' }, max_records: { type: 'number', description: 'Maximum number of results (1-10000, default: 100)', minimum: 1, maximum: 10000 }, }, required: ['query'], }, },
- src/index.ts:740-741 (registration)Dispatch registration in the CallToolRequestSchema switch statement, routing 'search_compounds' calls to the handleSearchCompounds method.case 'search_compounds': return await this.handleSearchCompounds(args);
- src/index.ts:373-381 (schema)JSON input schema defining the expected parameters for the 'search_compounds' tool: query (required string), optional search_type enum, and max_records.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query (compound name, CAS, formula, or identifier)' }, search_type: { type: 'string', enum: ['name', 'smiles', 'inchi', 'sdf', 'cid', 'formula'], description: 'Type of search to perform (default: name)' }, max_records: { type: 'number', description: 'Maximum number of results (1-10000, default: 100)', minimum: 1, maximum: 10000 }, }, required: ['query'], },
- src/index.ts:52-63 (helper)Type guard function for validating input arguments to search_compounds handler, checking query string, valid search_type enum, and max_records range.const isValidCompoundSearchArgs = ( args: any ): args is { query: string; search_type?: string; max_records?: number } => { return ( typeof args === 'object' && args !== null && typeof args.query === 'string' && args.query.length > 0 && (args.search_type === undefined || ['name', 'smiles', 'inchi', 'sdf', 'cid', 'formula'].includes(args.search_type)) && (args.max_records === undefined || (typeof args.max_records === 'number' && args.max_records > 0 && args.max_records <= 10000)) ); };