bc_query_open_targets_graphql
Execute GraphQL queries to retrieve data on targets, diseases, drugs, variants, and studies from the Open Targets platform.
Instructions
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.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query_string | Yes | GraphQL query string starting with 'query' keyword | |
| variables | No | Optional variables for the GraphQL query |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The main handler function for the tool 'query_open_targets_graphql'. It accepts a GraphQL query string and optional variables, then executes the query against the Open Targets API endpoint using the execute_graphql_query helper.
@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}"} - Input schema using Pydantic/Annotated types: 'query_string' is a required string, 'variables' is an optional dict.
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, - Registration of the tool via the @core_mcp.tool() decorator on the core_mcp FastMCP server instance.
@core_mcp.tool() def query_open_targets_graphql( - src/biocontext_kb/core/opentargets/__init__.py:3-6 (registration)Package-level re-export of the query_open_targets_graphql function from the opentargets module.
from ._query_open_targets_graphql import query_open_targets_graphql __all__ = [ "get_open_targets_graphql_schema", - Helper utility that performs the actual GraphQL query execution via the gql library. Used by the tool handler to make the API call.
def execute_graphql_query(endpoint_url, query_string, variables=None, headers=None) -> dict: """Make a generic GraphQL API call. Args: endpoint_url (str): The GraphQL endpoint URL query_string (str): The GraphQL query or mutation as a string variables (dict, optional): Variables for the GraphQL query headers (dict, optional): HTTP headers to include Returns: dict: The response data from the GraphQL API """ # Set default headers if none provided if headers is None: headers = { "Content-Type": "application/json", } # Prepare the transport transport = RequestsHTTPTransport( url=endpoint_url, headers=headers, use_json=True, ) # Create a client client = Client(transport=transport) # Parse the query string try: query = gql(query_string) except Exception as e: return {"status": "error", "message": f"Failed to parse query: {e!s}"} try: result = client.execute(query, variable_values=variables) return {"status": "success", "data": result} except Exception as e: return {"status": "error", "message": str(e)}