bc_query_open_targets_graphql
Execute GraphQL queries on the Open Targets API to retrieve biomedical data, including gene-disease associations, drug targets, and variant effects. Ensures valid queries by integrating schema and examples for accurate results. Supports Ensembl gene IDs and EFO disease IDs.
Instructions
Execute a GraphQL query against the Open Targets API after fetching the schema.
Important: Always first fetch examples using the schema using get_open_targets_query_examples. If the examples are
not sufficient, also get the schema using the get_open_targets_graphql_schema tool before executing a query.
Relying on either of these options provides the necessary context for the query and ensures that the query is valid.
Queries should use the Ensembl gene ID (e.g., "ENSG00000141510").
If necessary, first use get_ensembl_id_from_gene_symbol to convert gene symbols (e.g., "TP53") to Ensembl IDs.
If a disease ID is needed, use the get_efo_id_from_disease_name tool to get the EFO ID (e.g., "EFO_0004705") for a
disease name (e.g., "Hypothyroidism").
Make sure to always start the query string with the keyword query followed by the query name.
The query string should be a valid GraphQL query, and the variables should be a dictionary of parameters
that the query requires.
Open Targets provides data on:
target: annotations, tractability, mouse models, expression, disease/phenotype associations, available drugs.
disease: annotations, ontology, drugs, symptoms, target associations.
drug: annotations, mechanisms, indications, pharmacovigilance.
variant: annotations, frequencies, effects, consequences, credible sets.
studies: annotations, traits, publications, cohorts, credible sets.
credibleSet: annotations, variant sets, gene assignments, colocalization.
search: index of all platform entities.
Args: query_string (str): The GraphQL query string. variables (dict): The variables for the GraphQL query.
Returns: dict: The response data from the GraphQL API.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query_string | Yes | The GraphQL query string | |
| variables | No | The variables for the GraphQL query |
Implementation Reference
- The handler function decorated with @core_mcp.tool() that executes the GraphQL query against Open Targets API.@core_mcp.tool() def query_open_targets_graphql( query_string: Annotated[str, Field(description="GraphQL query string starting with 'query' keyword")], variables: Annotated[Optional[dict], Field(description="Optional variables for the GraphQL query")] = None, ) -> dict: """Execute GraphQL queries against the Open Targets API. Use get_open_targets_query_examples() or get_open_targets_graphql_schema() first. Returns: dict: GraphQL response with data field containing targets, diseases, drugs, variants, studies or error message. """ base_url = "https://api.platform.opentargets.org/api/v4/graphql" try: response = execute_graphql_query(base_url, query_string, variables) return response except Exception as e: return {"error": f"Failed to execute GraphQL query: {e!s}"}
- Companion tool to retrieve the GraphQL schema for constructing queries for the Open Targets API.@core_mcp.tool() def get_open_targets_graphql_schema() -> dict: """Retrieve the Open Targets GraphQL schema for query construction. Returns: dict: Schema string in format {'schema': '...'} containing GraphQL type definitions or error message. """ base_url = "https://api.platform.opentargets.org/api/v4/graphql" try: schema = fetch_graphql_schema(base_url) return {"schema": print_schema(schema)} except Exception as e: return {"error": f"Failed to fetch Open Targets GraphQL schema: {e!s}"}
- src/biocontext_kb/core/opentargets/__init__.py:1-3 (registration)Imports the Open Targets tools into the module, making them available for registration via core_mcp.from ._get_open_targets_graphql_schema import get_open_targets_graphql_schema from ._get_open_targets_query_examples import get_open_targets_query_examples from ._query_open_targets_graphql import query_open_targets_graphql
- src/biocontext_kb/core/__init__.py:14-14 (registration)Imports all Open Targets tools into core_mcp namespace.from .opentargets import *
- Companion tool providing example GraphQL queries for common use cases in Open Targets API.@core_mcp.tool() def get_open_targets_query_examples() -> dict: """Retrieve example GraphQL queries for the Open Targets API. Examples demonstrate common use cases. Returns: dict: Example queries mapped by category (informationForTarget, drugsForTarget, associatedDiseases, etc.) with GraphQL query strings. """ return EXAMPLE_QUERIES