get_chemical_image
Generate chemical structure images from SMILES or other notations, specifying height and width for precise visualization needs on the SureChEMBL MCP Server.
Instructions
Generate chemical structure image from SMILES or other structure notation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| height | No | Image height in pixels (default: 200) | |
| structure | Yes | SMILES string or other structure notation | |
| width | No | Image width in pixels (default: 200) |
Implementation Reference
- src/index.ts:803-843 (handler)The main handler function for the 'get_chemical_image' tool. It validates input, calls the SureChEMBL API to generate an image from the structure (SMILES), converts the binary image to base64 data URI, and returns it in the MCP response format.private async handleGetChemicalImage(args: any) { if (!isValidImageArgs(args)) { throw new McpError(ErrorCode.InvalidParams, 'Invalid image arguments'); } try { const height = args.height || 200; const width = args.width || 200; const response = await this.apiClient.get('/service/chemical/image', { params: { structure: args.structure, height: height, width: width }, responseType: 'arraybuffer' }); // Convert binary data to base64 for JSON response const base64Image = Buffer.from(response.data).toString('base64'); return { content: [ { type: 'text', text: JSON.stringify({ structure: args.structure, image_data: `data:image/png;base64,${base64Image}`, dimensions: { width, height }, message: 'Chemical structure image generated successfully' }, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to generate chemical image: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:438-446 (schema)The input schema definition for the 'get_chemical_image' tool, specifying parameters: structure (required string), optional height and width (numbers with bounds). Part of the tool registration in ListToolsRequestSchema.inputSchema: { type: 'object', properties: { structure: { type: 'string', description: 'SMILES string or other structure notation' }, height: { type: 'number', description: 'Image height in pixels (default: 200)', minimum: 50, maximum: 1000 }, width: { type: 'number', description: 'Image width in pixels (default: 200)', minimum: 50, maximum: 1000 }, }, required: ['structure'], },
- src/index.ts:435-447 (registration)The full tool registration entry in the tools list returned by ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'get_chemical_image', description: 'Generate chemical structure image from SMILES or other structure notation', inputSchema: { type: 'object', properties: { structure: { type: 'string', description: 'SMILES string or other structure notation' }, height: { type: 'number', description: 'Image height in pixels (default: 200)', minimum: 50, maximum: 1000 }, width: { type: 'number', description: 'Image width in pixels (default: 200)', minimum: 50, maximum: 1000 }, }, required: ['structure'], }, },
- src/index.ts:562-563 (registration)The switch case in CallToolRequestSchema handler that dispatches calls to the 'get_chemical_image' tool to its handler method.case 'get_chemical_image': return await this.handleGetChemicalImage(args);
- src/index.ts:129-140 (helper)Type guard function used to validate arguments for the get_chemical_image handler.const isValidImageArgs = ( args: any ): args is { structure: string; height?: number; width?: number } => { return ( typeof args === 'object' && args !== null && typeof args.structure === 'string' && args.structure.length > 0 && (args.height === undefined || (typeof args.height === 'number' && args.height > 0 && args.height <= 1000)) && (args.width === undefined || (typeof args.width === 'number' && args.width > 0 && args.width <= 1000)) ); };