Skip to main content
Glama

get-drug-by-generic-name

Retrieve all brand versions of a generic drug by entering its active ingredient name. Ideal for identifying alternative drug brands when only the generic name is known.

Instructions

Get drug information by generic (active ingredient) name. Useful when you know the generic name but not the brand name. Returns all brand versions of the generic drug.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
genericNameYesGeneric drug name (active ingredient)
limitNoMaximum number of results to return

Implementation Reference

  • The core handler function that executes the tool: builds OpenFDA query for generic name, fetches data, handles errors, maps results to simplified drug info, and returns formatted text response.
    async ({ genericName, limit }) => { const url = new OpenFDABuilder() .context("label") .search(`openfda.generic_name:"${genericName}"`) .limit(limit) .build(); const { data: drugData, error } = await makeOpenFDARequest<OpenFDAResponse>(url); if (error) { return { content: [{ type: "text", text: `Failed to retrieve drug data for generic name "${genericName}": ${error.message}`, }], }; } if (!drugData || !drugData.results || drugData.results.length === 0) { return { content: [{ type: "text", text: `No drug information found for generic name "${genericName}".`, }], }; } const drugs = drugData.results.map(drug => ({ brand_name: drug?.openfda.brand_name?.[0] || 'Unknown', generic_name: drug?.openfda.generic_name?.[0] || 'Unknown', manufacturer_name: drug?.openfda.manufacturer_name?.[0] || 'Unknown', product_type: drug?.openfda.product_type?.[0] || 'Unknown', route: drug?.openfda.route || [], })); return { content: [{ type: "text", text: `Found ${drugs.length} drug(s) with generic name "${genericName}":\n\n${JSON.stringify(drugs, null, 2)}`, }], }; }
  • Zod input schema defining parameters for the tool: required genericName string and optional limit number (default 5).
    { genericName: z.string().describe("Generic drug name (active ingredient)"), limit: z.number().optional().default(5).describe("Maximum number of results to return") },
  • src/index.ts:150-199 (registration)
    MCP server.tool() registration call that associates the tool name, description, input schema, and handler function.
    server.tool( "get-drug-by-generic-name", "Get drug information by generic (active ingredient) name. Useful when you know the generic name but not the brand name. Returns all brand versions of the generic drug.", { genericName: z.string().describe("Generic drug name (active ingredient)"), limit: z.number().optional().default(5).describe("Maximum number of results to return") }, async ({ genericName, limit }) => { const url = new OpenFDABuilder() .context("label") .search(`openfda.generic_name:"${genericName}"`) .limit(limit) .build(); const { data: drugData, error } = await makeOpenFDARequest<OpenFDAResponse>(url); if (error) { return { content: [{ type: "text", text: `Failed to retrieve drug data for generic name "${genericName}": ${error.message}`, }], }; } if (!drugData || !drugData.results || drugData.results.length === 0) { return { content: [{ type: "text", text: `No drug information found for generic name "${genericName}".`, }], }; } const drugs = drugData.results.map(drug => ({ brand_name: drug?.openfda.brand_name?.[0] || 'Unknown', generic_name: drug?.openfda.generic_name?.[0] || 'Unknown', manufacturer_name: drug?.openfda.manufacturer_name?.[0] || 'Unknown', product_type: drug?.openfda.product_type?.[0] || 'Unknown', route: drug?.openfda.route || [], })); return { content: [{ type: "text", text: `Found ${drugs.length} drug(s) with generic name "${genericName}":\n\n${JSON.stringify(drugs, null, 2)}`, }], }; } );
  • Helper class used by the handler to construct the OpenFDA API URL with context, search query for generic_name, and limit.
    * Licensed under the MIT License */ // The ContextType type defines the valid OpenFDA API contexts that can be used with the OpenFDABuilder. // These correspond to different OpenFDA drug endpoints, such as: // - 'ndc': National Drug Code Directory // - 'label': Drug Labeling // - 'event': Adverse Event Reporting type ContextType = "ndc" | "label" | "event"; /** * The OpenFDABuilder class helps construct URLs for the OpenFDA API. * * Usage: * - Set the context (such as 'label', 'ndc', or 'event') using the context() method. * - Set the search query using the search() method. * - Optionally set the result limit using the limit() method (default is 1). * - Call build() to assemble and return the final API URL. * * Example: * const url = new OpenFDABuilder() * .context('label') * .search('openfda.brand_name:"Advil"') * .limit(1) * .build(); * * The build() method will throw an error if any required parameter is missing. * The API key is read from the OPENFDA_API_KEY environment variable. */ export class OpenFDABuilder { private url = "https://api.fda.gov/drug/";
  • Helper function called by the handler to make the HTTP request to OpenFDA API and handle response/errors.
    | "network" | "http" | "parsing" | "timeout" | "empty_response" | "unknown"; message: string; status?: number; details?: any; } // Configuration for retry logic interface RequestConfig { maxRetries?: number; retryDelay?: number; timeout?: number; } const DEFAULT_CONFIG: RequestConfig = { maxRetries: 3, retryDelay: 1000, // 1 second timeout: 30000, // 30 seconds }; // Helper function to determine if error is retryable function isRetryableError(error: any): boolean { // Network errors, timeouts, and 5xx server errors are retryable if (error.name === "TypeError" && error.message.includes("fetch")) return true; if (error.name === "AbortError") return true; if (error.status >= 500 && error.status <= 599) return true; if (error.status === 429) return true; // Rate limit return false; } // Sleep utility for retry delays

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ythalorossy/openfda'

If you have feedback or need assistance with the MCP directory API, please join our Discord server