Skip to main content

Unofficial community-maintained Python client for the Open Targets Platform GraphQL API

Project description

opentargets-py

PyPI version Python versions CI License

Modern Python client for the Open Targets Platform GraphQL API — with sync and async clients, a --json CLI, and an MCP server so AI agents can query it directly.

Disclaimer: Unofficial, community-maintained. Not affiliated with or endorsed by the Open Targets consortium. For the official platform, visit platform.opentargets.org.

The official opentargets package was deprecated when Open Targets migrated to GraphQL in 2021 and has since been removed from PyPI. This library fills that gap — it is the only Python SDK targeting the current GraphQL API.

Installation

pip install opentargets-py                 # core SDK (sync + async)
pip install opentargets-py[pandas]         # adds DataFrame output
pip install opentargets-py[cli]            # adds the `opentargets` CLI
pip install opentargets-py[mcp]            # adds the `opentargets-mcp` MCP server
pip install opentargets-py[all]            # everything

Quick start

Sync

from opentargets import OpenTargetsClient

with OpenTargetsClient() as client:
    target = client.get_target("EGFR")
    print(target.approved_name)  # epidermal growth factor receptor

    associations = client.get_target_associations("EGFR", limit=10)
    for a in associations:
        print(a.disease_name, round(a.score, 3))

Async (concurrent fan-out)

import asyncio
from opentargets import AsyncOpenTargetsClient

async def main():
    async with AsyncOpenTargetsClient() as client:
        targets = await asyncio.gather(
            client.get_target("EGFR"),
            client.get_target("BRAF"),
            client.get_target("KRAS"),
        )
        for t in targets:
            print(t.approved_symbol, t.biotype)

asyncio.run(main())

CLI

opentargets target EGFR                          # rich table
opentargets target EGFR --json                   # machine-parseable
opentargets targets EGFR BRAF KRAS --json        # batch
opentargets disease EFO_0000311 --targets --json | jq '.targets[0]'
opentargets search "lung cancer" --json

Every subcommand supports --json, so the CLI is usable from shell scripts, agents, and notebooks alike.

MCP server (Claude Desktop, Cursor, etc.)

pip install opentargets-py[mcp]   # requires Python 3.10+

Then add to your claude_desktop_config.json (macOS path: ~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "opentargets": {
      "command": "opentargets-mcp"
    }
  }
}

Now you can ask Claude things like "What diseases is EGFR associated with?" and it will call the SDK directly. Exposed tools: get_target_info, find_target_associations, get_target_drugs, get_target_tractability, get_target_safety, get_target_expression, get_target_constraint, get_disease_info, find_disease_targets, get_drug_info, get_drug_indications, search_open_targets.

Features

  • Sync + async clientsOpenTargetsClient and AsyncOpenTargetsClient with full feature parity
  • 15 endpoint methods — targets, diseases, drugs, associations, tractability, safety liabilities, baseline expression, genetic constraint, ChEMBL crosslinks, search
  • Symbol resolution — pass "EGFR" instead of "ENSG00000146648"
  • Auto-pagination — fetches all pages transparently
  • Caching — in-memory TTLCache by default; opt into DiskCache (SQLite, survives process restarts) for repeated runs
  • Configurable retriesRetryConfig(max_retries=..., base_delay=..., retryable_statuses=..., respect_retry_after=...)
  • Pandas integrationas_dataframe=True on list-returning methods
  • Typed — full Pydantic v2 models, py.typed marker, mypy --strict compliant
  • CLIopentargets with --json on every command, rich tables for humans
  • MCP serveropentargets-mcp, drop-in for Claude Desktop / Cursor / any MCP host
  • Minimal core — just httpx + pydantic; everything else is an opt-in extra

Configuration

Disk cache (persist across runs)

from opentargets import OpenTargetsClient, DiskCache

cache = DiskCache(path="~/.cache/opentargets.db", ttl=86400)  # 1 day
with OpenTargetsClient(cache=cache) as client:
    target = client.get_target("EGFR")  # first run: API
    target = client.get_target("EGFR")  # second run (even new process): cache hit

Custom retry policy

from opentargets import OpenTargetsClient, RetryConfig

retry = RetryConfig(max_retries=5, base_delay=2.0, max_delay=120.0)
client = OpenTargetsClient(retry_config=retry)

For AI agents

This package was designed to be usable by both humans and LLM-based agents. Three deliberate affordances:

  1. MCP server (opentargets-mcp) — wraps every public method as an MCP tool with LLM-friendly docstrings. Configure once and the model calls Open Targets directly. See MCP setup above.
  2. CLI with --json — every subcommand emits clean JSON, so agents can shell out and parse the result with jq or json.loads.
  3. Typed Pydantic responses — every method returns a model with .model_dump() / .model_dump_json(), so programmatic agents get structured data without parsing.

See llms.txt for a skimmable one-page overview of the full API surface, optimized for LLM ingestion.

More examples

See the examples/ directory:

API reference

See docs/api-reference.md. All public methods on OpenTargetsClient (and the matching AsyncOpenTargetsClient) have docstrings with Args, Returns, and Example blocks.

Contributing

  1. Fork the repo and create a feature branch.
  2. Install dev dependencies: pip install -e ".[dev,all]"
  3. Run tests: pytest
  4. Run linting + formatting: ruff check src tests && ruff format src tests
  5. Type check: mypy src/opentargets
  6. Open a pull request.

License

Apache 2.0 — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opentargets_py-0.2.0.tar.gz (54.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opentargets_py-0.2.0-py3-none-any.whl (39.4 kB view details)

Uploaded Python 3

File details

Details for the file opentargets_py-0.2.0.tar.gz.

File metadata

  • Download URL: opentargets_py-0.2.0.tar.gz
  • Upload date:
  • Size: 54.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.8

File hashes

Hashes for opentargets_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7b707257e3b32cd3d32d0af74beffd0625f48c9af1bec68ae9175aeba1c7212f
MD5 07522715ff7bcd252e1aba886dc94f45
BLAKE2b-256 4a48c5e977acc703c24e28c5c5d9009a073718ae9f995906b94aa655c66de618

See more details on using hashes here.

File details

Details for the file opentargets_py-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: opentargets_py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 39.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.8

File hashes

Hashes for opentargets_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13ab4ac9b0a3b6415b075ed0d7e721fee73d4565104eb7af909ba3ad85f6ca2a
MD5 d95f2ddc8eddb994c44ec528c38b1f0c
BLAKE2b-256 5915ec514cded879add82be4b6ac2d274b7ef851813b7af2c111df5526172244

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page