search_by_smiles
Find chemical compounds in the PubChem database using a SMILES string for exact-match searches. Retrieve molecular properties and bioassay data with precision.
Instructions
Search for compounds by SMILES string (exact match)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| smiles | Yes | SMILES string of the query molecule |
Implementation Reference
- src/index.ts:907-947 (handler)The handler function that executes the 'search_by_smiles' tool. It validates input using isValidSmilesArgs, queries the PubChem API for exact SMILES match to get CID, fetches compound details, and returns formatted results or error messages.private async handleSearchBySmiles(args: any) { if (!isValidSmilesArgs(args)) { throw new McpError(ErrorCode.InvalidParams, 'Invalid SMILES arguments'); } try { const response = await this.apiClient.get(`/compound/smiles/${encodeURIComponent(args.smiles)}/cids/JSON`); if (response.data?.IdentifierList?.CID?.length > 0) { const cid = response.data.IdentifierList.CID[0]; const detailsResponse = await this.apiClient.get(`/compound/cid/${cid}/property/MolecularFormula,MolecularWeight,CanonicalSMILES,IUPACName/JSON`); return { content: [ { type: 'text', text: JSON.stringify({ query_smiles: args.smiles, found_cid: cid, details: detailsResponse.data, }, null, 2), }, ], }; } return { content: [ { type: 'text', text: JSON.stringify({ message: 'No exact match found', query_smiles: args.smiles }, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to search by SMILES: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:398-404 (schema)Input schema definition for the 'search_by_smiles' tool, specifying a required 'smiles' string parameter.inputSchema: { type: 'object', properties: { smiles: { type: 'string', description: 'SMILES string of the query molecule' }, }, required: ['smiles'], },
- src/index.ts:395-405 (registration)Registration of the 'search_by_smiles' tool in the ListToolsRequestSchema response, including name, description, and input schema.{ name: 'search_by_smiles', description: 'Search for compounds by SMILES string (exact match)', inputSchema: { type: 'object', properties: { smiles: { type: 'string', description: 'SMILES string of the query molecule' }, }, required: ['smiles'], }, },
- src/index.ts:76-87 (helper)Type guard and validation function for SMILES search arguments, used in the handler to validate input parameters.const isValidSmilesArgs = ( args: any ): args is { smiles: string; threshold?: number; max_records?: number } => { return ( typeof args === 'object' && args !== null && typeof args.smiles === 'string' && args.smiles.length > 0 && (args.threshold === undefined || (typeof args.threshold === 'number' && args.threshold >= 0 && args.threshold <= 100)) && (args.max_records === undefined || (typeof args.max_records === 'number' && args.max_records > 0 && args.max_records <= 10000)) ); };