Skip to main content

Minimal Python client for Datagen tools API

Project description

datagen-python-sdk

A minimal Python SDK for interacting with DataGen MCP (Model Context Protocol) APIs. This SDK provides a simple, type-safe interface to execute tools via the Datagen API with built-in authentication, retry logic, and comprehensive error handling.

Features

  • Simple, intuitive API for executing DataGen tools
  • Automatic authentication via API key
  • Built-in retry logic with exponential backoff
  • Comprehensive error handling with specific exception types
  • Type hints for better IDE support
  • Zero dependencies except requests

Installation

Install from PyPI:

pip install datagen-python-sdk

Quick Start

1. Set up your API key

You can provide your API key in two ways:

Option A: Environment variable (recommended)

export DATAGEN_API_KEY=your_api_key_here

Option B: Pass directly to the client

from datagen_sdk import DatagenClient

client = DatagenClient(api_key="your_api_key_here")

2. Create a client and execute tools

from datagen_sdk import DatagenClient

# Initialize the client
client = DatagenClient(base_url="http://localhost:3001")

# Execute a tool
result = client.execute_tool(
    "mcp_Linear_list_projects",
    {"limit": 20}
)

print(f"Found {len(result[0]['content'])} projects")

API Reference

DatagenClient

The main client class for interacting with the Datagen API.

Constructor

DatagenClient(
    api_key: Optional[str] = None,
    base_url: str = "http://localhost:3001",
    timeout: int = 30,
    retries: int = 0,
    backoff_seconds: float = 0.5
)

Parameters:

  • api_key (str, optional): Your Datagen API key. If not provided, reads from DATAGEN_API_KEY environment variable.
  • base_url (str, optional): The base URL of your Datagen API instance. Default: http://localhost:3001
  • timeout (int, optional): Request timeout in seconds. Default: 30
  • retries (int, optional): Number of retry attempts for failed requests. Default: 0
  • backoff_seconds (float, optional): Base backoff time for exponential retry. Default: 0.5

Raises:

  • DatagenAuthError: If API key is not provided and DATAGEN_API_KEY is not set

Methods

execute_tool

Execute a DataGen tool by its alias name.

execute_tool(
    tool_alias_name: str,
    parameters: Optional[Dict[str, Any]] = None
) -> Any

Parameters:

  • tool_alias_name (str): The alias name of the tool to execute (e.g., "mcp_Linear_list_projects")
  • parameters (dict, optional): Parameters to pass to the tool. Default: {}

Returns:

  • The result data from the tool execution

Raises:

  • DatagenAuthError: Authentication failed (401/403)
  • DatagenHttpError: HTTP-level errors (network issues, 4xx/5xx status codes)
  • DatagenToolError: Tool executed but reported a failure

Error Handling

The SDK provides specific exception types for different error scenarios:

from datagen_sdk import (
    DatagenClient,
    DatagenError,          # Base exception
    DatagenAuthError,      # Authentication errors
    DatagenHttpError,      # HTTP-level errors
    DatagenToolError       # Tool execution errors
)

try:
    client = DatagenClient()
    result = client.execute_tool("my_tool", {"param": "value"})
except DatagenAuthError as e:
    print(f"Authentication failed: {e}")
except DatagenToolError as e:
    print(f"Tool execution failed: {e}")
except DatagenHttpError as e:
    print(f"HTTP error: {e}")
except DatagenError as e:
    print(f"General error: {e}")

Configuration

Using Environment Variables

export DATAGEN_API_KEY=your_api_key_here

Client Configuration Examples

Basic usage:

client = DatagenClient()

Custom base URL:

client = DatagenClient(base_url="https://api.datagen.dev")

With retry logic:

client = DatagenClient(
    retries=3,              # Retry up to 3 times
    backoff_seconds=1.0     # Start with 1 second, doubles each retry
)

Custom timeout:

client = DatagenClient(timeout=60)  # 60 second timeout

Examples

List Projects

from datagen_sdk import DatagenClient

client = DatagenClient(base_url="http://localhost:3001")
projects = client.execute_tool("mcp_Linear_list_projects", {"limit": 20})

print(f"Projects returned: {len(projects[0]['content']) if projects else 0}")
for proj in projects[0]["content"]:
    print(f"- {proj.get('name')} (id: {proj.get('id')})")

List Issues

from datagen_sdk import DatagenClient

client = DatagenClient(base_url="http://localhost:3001")
issues = client.execute_tool(
    "mcp_Linear_list_issues",
    {
        "limit": 10,
        "order_by": "createdAt",
        "order_direction": "DESC",
    }
)

issue_block = issues[0] if issues else []
print(f"Issues returned: {len(issue_block)}")
for issue in issue_block:
    ident = issue.get("identifier", "?")
    title = issue.get("title", "(no title)")
    print(f"- {ident}: {title}")

Error Handling with Retries

from datagen_sdk import DatagenClient, DatagenHttpError

client = DatagenClient(
    base_url="http://localhost:3001",
    retries=3,
    backoff_seconds=0.5
)

try:
    result = client.execute_tool("my_tool", {"param": "value"})
    print("Success:", result)
except DatagenHttpError as e:
    print(f"Failed after retries: {e}")

Development

Running Examples

See the examples/ directory for complete examples:

# Set your API key
export DATAGEN_API_KEY=your_api_key_here

# Run examples
python examples/list_projects.py
python examples/list_issues.py

Requirements

  • Python >= 3.10
  • requests >= 2.31.0

License

This project is licensed under the MIT License - see the LICENSE file for details.

Links

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

datagen_python_sdk-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

datagen_python_sdk-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file datagen_python_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: datagen_python_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for datagen_python_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c2734ea0692c2ab5aebc10effc1d3ff39673e81bb621468f000f9342e8bb7444
MD5 e46b75f1a064243d75da730887ca5eec
BLAKE2b-256 d0c18abb308d6a4a00ceab9c10d5596ce80ec487b70ab2731ce82c6f99d8d4fc

See more details on using hashes here.

File details

Details for the file datagen_python_sdk-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for datagen_python_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb2cec714c9cfd8e94d5dba03323434d1282fd25cc711ad4d21cd571c962bac8
MD5 9885b21e2517f1cd3212973cf68cd40d
BLAKE2b-256 14172d32c585fd89e1387686018fb3a2e812c4bc9ef4217995ebe577494dadce

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