tooluniverse.execute_function module

ToolUniverse Function Execution Module

This module provides the core ToolUniverse class for managing and executing various scientific and data tools. It supports loading tools from JSON configurations, organizing them by categories, validating function calls, and executing tools with proper error handling and caching.

The module includes support for: - GraphQL tools (OpenTarget, OpenTarget Genetics) - RESTful API tools (Monarch, ChEMBL, PubChem, etc.) - FDA drug labeling and adverse event tools - Clinical trials tools - Literature search tools (EuropePMC, Semantic Scholar, PubTator) - Biological databases (HPA, Reactome, UniProt) - MCP (Model Context Protocol) clients and auto-loaders - Enrichment analysis tools - Package management tools

Classes:

ToolUniverse: Main class for tool management and execution

Constants:

default_tool_files: Default mapping of tool categories to JSON file paths tool_type_mappings: Mapping of tool type strings to their implementation classes

class tooluniverse.execute_function._BatchCacheInfo[source]

Bases: object

_BatchCacheInfo(namespace: str, version: str, cache_key: str)

namespace: str
version: str
cache_key: str
__init__(namespace, version, cache_key)
class tooluniverse.execute_function._BatchJob[source]

Bases: object

_BatchJob(signature: str, call: Dict[str, Any], function_name: str, arguments: Dict[str, Any], indices: List[int] = <factory>, tool_instance: Any = None, cache_info: Optional[tooluniverse.execute_function._BatchCacheInfo] = None, cache_key_composed: Optional[str] = None, skip_execution: bool = False)

signature: str
call: Dict[str, Any]
function_name: str
arguments: Dict[str, Any]
indices: List[int]
tool_instance: Any = None
cache_info: _BatchCacheInfo | None = None
cache_key_composed: str | None = None
skip_execution: bool = False
__init__(signature, call, function_name, arguments, indices=<factory>, tool_instance=None, cache_info=None, cache_key_composed=None, skip_execution=False)
class tooluniverse.execute_function.ToolCallable[source]

Bases: object

A callable wrapper for a tool that validates kwargs and calls run_one_function.

This class provides the dynamic function interface for tools, allowing them to be called like regular Python functions with keyword arguments.

__init__(engine, tool_name)[source]
__call__(*, stream_callback=None, use_cache=False, validate=True, **kwargs)[source]

Execute the tool with the provided keyword arguments.

Parameters:
  • stream_callback – Optional callback for streaming responses

  • use_cache – Whether to use result caching

  • validate – Whether to validate parameters against schema

  • **kwargs – Tool-specific arguments

Returns:

Tool execution result

class tooluniverse.execute_function.ToolNamespace[source]

Bases: object

Dynamic namespace for accessing tools as callable functions.

This class provides the tu.tools.tool_name(**kwargs) interface, dynamically creating ToolCallable instances for each available tool.

__init__(engine)[source]
__getattr__(name)[source]

Return a ToolCallable for the requested tool name.

__len__()[source]

Return the number of available tools.

__iter__()[source]

Iterate over tool names.

__contains__(name)[source]

Check if a tool exists.

refresh()[source]

Refresh tool discovery (re-discover MCP/remote tools).

eager_load(names=None)[source]

Pre-instantiate tools to reduce first-call latency.

class tooluniverse.execute_function.ToolUniverse[source]

Bases: object

A comprehensive tool management system for loading, organizing, and executing various scientific and data tools.

The ToolUniverse class provides a centralized interface for managing different types of tools including GraphQL tools, RESTful APIs, MCP clients, and specialized scientific tools. It handles tool loading, filtering, caching, and execution.

all_tools

List of all loaded tool configurations

Type:

list

all_tool_dict

Dictionary mapping tool names to their configurations

Type:

dict

tool_category_dicts

Dictionary organizing tools by category

Type:

dict

tool_files

Dictionary mapping category names to their JSON file paths

Type:

dict

callable_functions

Cache of instantiated tool objects

Type:

dict

__init__(tool_files={'ChEMBL': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/chembl_tools.json', 'EFO': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/efo_tools.json', 'Enrichr': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/enrichr_tools.json', 'EuropePMC': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/europe_pmc_tools.json', 'HumanBase': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/humanbase_tools.json', 'OpenAlex': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/openalex_tools.json', 'admetai': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/admetai_tools.json', 'adverse_event': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/adverse_event_tools.json', 'agents': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/agentic_tools.json', 'alphafold': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/alphafold_tools.json', 'arxiv': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/arxiv_tools.json', 'biorxiv': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/biorxiv_tools.json', 'cellosaurus': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/cellosaurus_tools.json', 'clinical_trials': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/clinicaltrials_gov_tools.json', 'compose': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/compose_tools.json', 'core': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/core_tools.json', 'crossref': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/crossref_tools.json', 'dailymed': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/dailymed_tools.json', 'dataset': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/dataset_tools.json', 'dblp': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/dblp_tools.json', 'disease_target_score': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/disease_target_score_tools.json', 'doaj': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/doaj_tools.json', 'drug_discovery_agents': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/drug_discovery_agents.json', 'embedding': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/embedding_tools.json', 'fatcat': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/fatcat_tools.json', 'fda_drug_adverse_event': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/fda_drug_adverse_event_tools.json', 'fda_drug_label': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/fda_drug_labeling_tools.json', 'genomics': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/genomics_tools.json', 'go': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/gene_ontology_tools.json', 'guidelines': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/unified_guideline_tools.json', 'gwas': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/gwas_tools.json', 'hal': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/hal_tools.json', 'hpa': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/hpa_tools.json', 'idmap': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/idmap_tools.json', 'markitdown': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/markitdown_tools.json', 'mcp_auto_loader_boltz': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/boltz_tools.json', 'mcp_auto_loader_expert_feedback': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/expert_feedback_tools.json', 'mcp_auto_loader_txagent': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/txagent_client_tools.json', 'mcp_auto_loader_uspto_downloader': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/uspto_downloader_tools.json', 'medlineplus': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/medlineplus_tools.json', 'medrxiv': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/medrxiv_tools.json', 'monarch': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/monarch_tools.json', 'odphp': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/odphp_tools.json', 'openaire': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/openaire_tools.json', 'opentarget': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/opentarget_tools.json', 'osf_preprints': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/osf_preprints_tools.json', 'output_summarization': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/output_summarization_tools.json', 'pmc': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/pmc_tools.json', 'pubchem': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/pubchem_tools.json', 'pubmed': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/pubmed_tools.json', 'pubtator': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/pubtator_tools.json', 'rcsb_pdb': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/rcsb_pdb_tools.json', 'reactome': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/reactome_tools.json', 'semantic_scholar': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/semantic_scholar_tools.json', 'software_bioinformatics': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/bioinformatics_core_tools.json', 'software_cheminformatics': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/cheminformatics_tools.json', 'software_earth_sciences': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/earth_sciences_tools.json', 'software_genomics': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/genomics_tools.json', 'software_image_processing': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/image_processing_tools.json', 'software_machine_learning': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/machine_learning_tools.json', 'software_neuroscience': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/neuroscience_tools.json', 'software_physics_astronomy': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/physics_astronomy_tools.json', 'software_scientific_computing': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/scientific_computing_tools.json', 'software_single_cell': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/single_cell_tools.json', 'software_structural_biology': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/structural_biology_tools.json', 'software_visualization': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/packages/visualization_tools.json', 'special_tools': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/special_tools.json', 'tool_composition': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/tool_composition_tools.json', 'tool_finder': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/finder_tools.json', 'uniprot': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/uniprot_tools.json', 'unpaywall': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/unpaywall_tools.json', 'url': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/url_fetch_tools.json', 'uspto': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/uspto_tools.json', 'visualization_molecule_2d': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/molecule_2d_tools.json', 'visualization_molecule_3d': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/molecule_3d_tools.json', 'visualization_protein_3d': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/protein_structure_3d_tools.json', 'wikidata_sparql': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/wikidata_sparql_tools.json', 'xml': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/xml_tools.json', 'zenodo': '/home/runner/work/ToolUniverse/ToolUniverse/src/tooluniverse/data/zenodo_tools.json'}, keep_default_tools=True, log_level=None, hooks_enabled=False, hook_config=None, hook_type=None)[source]

Initialize the ToolUniverse with tool file configurations.

Parameters:
  • tool_files (dict, optional) – Dictionary mapping category names to JSON file paths. Defaults to default_tool_files.

  • keep_default_tools (bool, optional) – Whether to keep default tools when custom tool_files are provided. Defaults to True.

  • log_level (str, optional) – Log level for this instance. Can be ‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’. If None, uses global setting.

  • hooks_enabled (bool, optional) – Whether to enable output hooks. Defaults to False.

  • hook_config (dict, optional) – Configuration for hooks. If None, uses default config.

  • hook_type (str or list, optional) – Simple hook type selection. Can be ‘SummarizationHook’, ‘FileSaveHook’, or a list of both. Defaults to ‘SummarizationHook’. If both hook_config and hook_type are provided, hook_config takes precedence.

register_custom_tool(tool_class, tool_name=None, tool_config=None)[source]

Register a custom tool class at runtime.

Parameters:
  • tool_class – The tool class to register

  • tool_name (str, optional) – Name to register under. Uses class name if None.

  • tool_config (dict, optional) – Tool configuration dictionary to add to all_tools

Returns:

The name the tool was registered under

Return type:

str

force_full_discovery()[source]

Force full tool discovery, importing all tool modules immediately.

This can be useful when you need to ensure all tools are available immediately, bypassing lazy loading.

Returns:

Updated tool registry with all discovered tools

Return type:

dict

get_lazy_loading_status()[source]

Get information about lazy loading status and available tools.

Returns:

Dictionary with lazy loading status and tool counts

Return type:

dict

get_tool_types()[source]

Get the types of tools available in the tool files.

Returns:

A list of tool type names (category keys).

Return type:

list

_get_api_key(key_name)[source]

Get API key from environment variables or loaded sources

_check_api_key_requirements(tool_config)[source]

Check if a tool’s required API keys are available. Also supports optional_api_keys where at least one key from the list must be available.

Parameters:

tool_config (dict) – Tool configuration containing optional ‘required_api_keys’ and ‘optional_api_keys’ fields

Returns:

(bool, list) - (all_keys_available, missing_keys)

Return type:

tuple

generate_env_template(all_missing_keys, output_file='.env.template')[source]

Generate a template .env file with all required API keys

_create_hook_config_from_type(hook_type)[source]

Create hook configuration from simple hook_type parameter.

Parameters:

hook_type (str or list) – Hook type(s) to enable. Can be ‘SummarizationHook’, ‘FileSaveHook’, or a list of both.

Returns:

Generated hook configuration

Return type:

dict

load_tools(tool_type=None, exclude_tools=None, exclude_categories=None, include_tools=None, tool_config_files=None, tools_file=None, include_tool_types=None, exclude_tool_types=None)[source]

Loads tool definitions from JSON files into the instance’s tool registry.

If tool_type is None, loads all available tool categories from self.tool_files. Otherwise, loads only the specified tool categories.

After loading, deduplicates tools by their ‘name’ field and updates the internal tool list. Also refreshes the tool name and description mapping.

Parameters:
  • tool_type (list, optional) – List of tool category names to load. If None, loads all categories.

  • exclude_tools (list, optional) – List of specific tool names to exclude from loading.

  • exclude_categories (list, optional) – List of tool categories to exclude from loading.

  • include_tools (list or str, optional) – List of specific tool names to include, or path to a text file containing tool names (one per line). If provided, only these tools will be loaded regardless of categories.

  • tool_config_files (dict, optional) – Additional tool configuration files to load. Format: {“category_name”: “/path/to/config.json”}

  • tools_file (str, optional) – Path to a text file containing tool names to include (one per line). Alternative to include_tools when providing a file path.

  • include_tool_types (list, optional) – List of tool types to include (e.g., [“OpenTarget”, “ChEMBLTool”]). If provided, only tools with these types will be loaded.

  • exclude_tool_types (list, optional) – List of tool types to exclude (e.g., [“ToolFinderEmbedding”]). Tools with these types will be excluded.

Side Effects:
  • Updates self.all_tools with loaded and deduplicated tools.

  • Updates self.tool_category_dicts with loaded tools per category.

  • Calls self.refresh_tool_name_desc() to update tool name/description mapping.

  • Prints the number of tools before and after loading.

Examples

# Load specific tools by name tu.load_tools(include_tools=[“UniProt_get_entry_by_accession”, “ChEMBL_get_molecule_by_chembl_id”])

# Load tools from a file tu.load_tools(tools_file=”/path/to/tool_names.txt”)

# Include only specific tool types tu.load_tools(include_tool_types=[“OpenTarget”, “ChEMBLTool”])

# Exclude specific tool types tu.load_tools(exclude_tool_types=[“ToolFinderEmbedding”, “Unknown”])

# Load additional config files tu.load_tools(tool_config_files={“custom_tools”: “/path/to/custom_tools.json”})

# Combine multiple options tu.load_tools(

tool_type=[“uniprot”, “ChEMBL”], exclude_tools=[“problematic_tool”], exclude_tool_types=[“Unknown”], tool_config_files={“custom”: “/path/to/custom.json”}

)

_load_tool_names_from_file(file_path)[source]

Load tool names from a text file (one tool name per line).

Parameters:

file_path (str) – Path to the text file containing tool names

Returns:

List of tool names loaded from the file

Return type:

list

_filter_and_deduplicate_tools(exclude_tools_set, include_tools_set, include_tool_types_set=None, exclude_tool_types_set=None)[source]

Filter tools based on inclusion/exclusion criteria and remove duplicates.

Parameters:
  • exclude_tools_set (set) – Set of tool names to exclude

  • include_tools_set (set or None) – Set of tool names to include (if None, include all)

  • include_tool_types_set (set or None) – Set of tool types to include (if None, include all)

  • exclude_tool_types_set (set or None) – Set of tool types to exclude (if None, exclude none)

_load_auto_discovered_configs()[source]

Load auto-discovered configs from the decorator registry.

This method loads tool configurations that were registered automatically via the @register_tool decorator with config parameter.

_process_mcp_auto_loaders()[source]

Process any MCPAutoLoaderTool instances to automatically discover and register MCP tools.

This method scans through all loaded tools for MCPAutoLoaderTool instances and runs their auto-discovery process to find and register MCP tools from configured servers. It handles async operations properly with cleanup and error handling.

Side Effects:
  • May add new tools to the tool registry

  • Prints debug information about the discovery process

  • Updates tool counts after MCP registration

list_built_in_tools(mode='config', scan_all=False)[source]

List all built-in tool categories and their statistics with different modes.

This method provides a comprehensive overview of all available tools in the ToolUniverse, organized by categories. It reads directly from the default tool files to gather statistics, so it works even before calling load_tools().

Parameters:
  • mode (str, optional) – Organization mode for tools. Defaults to ‘config’. - ‘config’: Organize by config file categories (original behavior) - ‘type’: Organize by tool types (implementation classes) - ‘list_name’: Return a list of all tool names - ‘list_spec’: Return a list of all tool specifications

  • scan_all (bool, optional) – Whether to scan all JSON files in data directory recursively. If True, scans all JSON files in data/ and its subdirectories. If False (default), uses predefined tool file mappings.

Returns:

  • For ‘config’ and ‘type’ modes: A dictionary containing tool statistics

  • For ‘list_name’ mode: A list of all tool names

  • For ‘list_spec’ mode: A list of all tool specifications

Return type:

dict or list

Example

>>> tool_universe = ToolUniverse()
>>> # Group by config file categories (predefined files only)
>>> stats = tool_universe.list_built_in_tools(mode='config')
>>> # Scan all JSON files in data directory recursively
>>> stats = tool_universe.list_built_in_tools(mode='config', scan_all=True)
>>> # Get all tool names from all JSON files
>>> tool_names = tool_universe.list_built_in_tools(mode='list_name', scan_all=True)

Note

  • This method reads directly from tool files and works without calling load_tools()

  • Tools are deduplicated across categories, so the same tool won’t be counted multiple times

  • The summary is automatically printed to console when this method is called (except for list_name and list_spec modes)

  • When scan_all=True, all JSON files in data/ and subdirectories are scanned

_read_tools_from_file(file_path)[source]

Read tools from a single JSON file with error handling.

Parameters:

file_path (str) – Path to the JSON file

Returns:

List of tool configurations from the file

Return type:

list

_scan_predefined_files()[source]

Scan predefined tool files (original behavior).

Returns:

(all_tools, all_tool_names) where all_tools is a list of tool configs

and all_tool_names is a set of tool names for deduplication

Return type:

tuple

_scan_all_json_files()[source]

Recursively scan all JSON files in the data directory and its subdirectories.

Returns:

(all_tools, all_tool_names) where all_tools is a list of tool configs

and all_tool_names is a set of tool names for deduplication

Return type:

tuple

refresh_tool_name_desc(enable_full_desc=False, include_names=None, exclude_names=None, include_categories=None, exclude_categories=None)[source]

Refresh the tool name and description mappings with optional filtering.

This method rebuilds the internal tool dictionary and generates filtered lists of tool names and descriptions based on the provided filter criteria.

Parameters:
  • enable_full_desc (bool, optional) – If True, includes full tool JSON as description. If False, uses “name: description” format. Defaults to False.

  • include_names (list, optional) – List of tool names to include.

  • exclude_names (list, optional) – List of tool names to exclude.

  • include_categories (list, optional) – List of categories to include.

  • exclude_categories (list, optional) – List of categories to exclude.

Returns:

A tuple containing (tool_name_list, tool_desc_list) after filtering.

Return type:

tuple

prepare_one_tool_prompt(tool)[source]

Prepare a single tool configuration for prompt usage by filtering to essential keys.

Parameters:

tool (dict) – Tool configuration dictionary.

Returns:

Tool configuration with only essential keys for prompting.

Return type:

dict

prepare_tool_prompts(tool_list, mode='prompt', valid_keys=None)[source]

Prepare a list of tool configurations for different usage modes.

Parameters:
  • tool_list (list) – List of tool configuration dictionaries.

  • mode (str) – Preparation mode. Options: - ‘prompt’: Keep essential keys for prompting (name, description, parameter, required) - ‘example’: Keep extended keys for examples (name, description, parameter, required, query_schema, fields, label, type) - ‘custom’: Use custom valid_keys parameter

  • valid_keys (list, optional) – Custom list of keys to keep when mode=’custom’.

Returns:

List of tool configurations with only specified keys.

Return type:

list

get_tool_specification_by_names(tool_names, format='default')[source]

Retrieve tool specifications by their names using tool_specification method.

Parameters:
  • tool_names (list) – List of tool names to retrieve.

  • format (str, optional) – Output format. Options: ‘default’, ‘openai’. If ‘openai’, returns OpenAI function calling format. Defaults to ‘default’.

Returns:

List of tool specifications for the specified names.

Tools not found will be reported but not included in the result.

Return type:

list

get_one_tool_by_one_name(tool_name, return_prompt=True)[source]

Retrieve a single tool specification by name, optionally prepared for prompting.

This is a convenience method that calls get_one_tool_by_one_name.

Parameters:
  • tool_name (str) – Name of the tool to retrieve.

  • return_prompt (bool, optional) – If True, returns tool prepared for prompting. If False, returns full tool configuration. Defaults to True.

Returns:

Tool configuration if found, None otherwise.

Return type:

dict or None

tool_specification(tool_name, return_prompt=False, format='default')[source]

Retrieve a single tool configuration by name.

Parameters:
  • tool_name (str) – Name of the tool to retrieve.

  • return_prompt (bool, optional) – If True, returns tool prepared for prompting. If False, returns full tool configuration. Defaults to False.

  • format (str, optional) – Output format. Options: ‘default’, ‘openai’. If ‘openai’, returns OpenAI function calling format. Defaults to ‘default’.

Returns:

Tool configuration if found, None otherwise.

Return type:

dict or None

get_tool_type_by_name(tool_name)[source]

Get the type of a tool by its name.

Parameters:

tool_name (str) – Name of the tool.

Returns:

The type of the tool.

Return type:

str

Raises:

KeyError – If the tool name is not found in loaded tools.

call_id_gen()[source]

Generate a random call ID for function calls.

Returns:

A random 9-character string composed of letters and digits.

Return type:

str

tool_to_str(tool_list)[source]

Convert a list of tool configurations to a formatted string.

Parameters:

tool_list (list) – List of tool configuration dictionaries.

Returns:

JSON-formatted string representation of the tools, with each tool

separated by double newlines.

Return type:

str

extract_function_call_json(lst, return_message=False, verbose=True, format='llama')[source]

Extract function call JSON from input data.

This method delegates to the utility function extract_function_call_json.

Parameters:
  • lst – Input data containing function call information.

  • return_message (bool, optional) – Whether to return message along with JSON. Defaults to False.

  • verbose (bool, optional) – Whether to enable verbose output. Defaults to True.

  • format (str, optional) – Format type for extraction. Defaults to ‘llama’.

Returns:

Function call JSON, optionally with message if return_message is True.

Return type:

dict or tuple

return_all_loaded_tools()[source]

Return a deep copy of all loaded tools.

Returns:

A deep copy of the all_tools list to prevent external modification.

Return type:

list

_execute_function_call_list(function_calls, stream_callback=None, use_cache=False, max_workers=None)[source]

Execute a list of function calls, optionally in parallel.

Parameters:
  • function_calls (List[Dict[str, Any]]) – Ordered list of function call dictionaries.

  • stream_callback – Optional streaming callback.

  • use_cache (bool) – Whether to enable cache lookups for each call.

  • max_workers (int | None) – Maximum parallel workers; values <=1 fall back to sequential execution.

Returns:

List of results aligned with function_calls order.

Return type:

List[Any]

_build_batch_jobs(function_calls)[source]
_prime_batch_cache(jobs, use_cache, results)[source]
_execute_batch_jobs(jobs_to_run, results, *, stream_callback, use_cache, max_workers)[source]
_ensure_tool_instance(job)[source]
_get_tool_semaphore(job, tool_semaphores)[source]
run(fcall_str, return_message=False, verbose=True, format='llama', stream_callback=None, use_cache=False, max_workers=None)[source]

Execute function calls from input string or data.

This method parses function call data, validates it, and executes the corresponding tools. It supports both single function calls and multiple function calls in a list.

Parameters:
  • fcall_str – Input string or data containing function call information.

  • return_message (bool, optional) – Whether to return formatted messages. Defaults to False.

  • verbose (bool, optional) – Whether to enable verbose output. Defaults to True.

  • format (str, optional) – Format type for parsing. Defaults to ‘llama’.

Returns:

  • For multiple function calls: List of formatted messages with tool responses

  • For single function call: Direct result from the tool

  • None: If the input is not a valid function call

Return type:

list or str or None

run_one_function(function_call_json, stream_callback=None, use_cache=False, validate=True)[source]

Execute a single function call.

This method validates the function call, initializes the tool if necessary, and executes it with the provided arguments. If hooks are enabled, it also applies output hooks to process the result.

Parameters:
  • function_call_json (dict) – Dictionary containing function name and arguments.

  • stream_callback (callable, optional) – Callback for streaming responses.

  • use_cache (bool, optional) – Whether to use result caching. Defaults to False.

  • validate (bool, optional) – Whether to validate parameters against schema. Defaults to True.

Returns:

Result from the tool execution, or error message if validation fails.

Return type:

str or dict

_execute_tool_with_stream(tool_instance, arguments, stream_callback, use_cache=False, validate=True)[source]

Invoke a tool, forwarding stream callbacks and other parameters when supported.

toggle_hooks(enabled)[source]

Enable or disable output hooks globally.

This method allows runtime control of the hook system. When enabled, it initializes the HookManager if not already present. When disabled, it deactivates the HookManager.

Parameters:

enabled (bool) – True to enable hooks, False to disable

init_tool(tool=None, tool_name=None, add_to_cache=True)[source]

Initialize a tool instance from configuration or name.

This method creates a new tool instance using the tool type mappings and optionally caches it for future use. It handles special cases like the OpentargetToolDrugNameMatch which requires additional dependencies.

Parameters:
  • tool (dict, optional) – Tool configuration dictionary. Either this or tool_name must be provided.

  • tool_name (str, optional) – Name of the tool type to initialize. Either this or tool must be provided.

  • add_to_cache (bool, optional) – Whether to cache the initialized tool. Defaults to True.

Returns:

Initialized tool instance or None if initialization fails.

Return type:

object

Raises:

KeyError – If the tool type is not found in tool_type_mappings.

_get_tool_instance(function_name, cache=True)[source]

Get or create tool instance with optional caching.

_make_cache_key(function_name, arguments)[source]

Generate cache key by delegating to BaseTool.

_validate_parameters(function_name, arguments)[source]

Validate parameters by delegating to BaseTool.

_check_basic_type(value, expected_type)[source]

Check if value matches expected basic type.

_classify_exception(exception, function_name, arguments)[source]

Classify exception by delegating to BaseTool.

_create_dual_format_error(error)[source]

Create dual-format error response for backward compatibility.

refresh_tools()[source]

Refresh tool discovery (re-discover MCP/remote tools, reload configs).

eager_load_tools(names=None)[source]

Pre-instantiate tools to reduce first-call latency.

property _cache

Access to the internal cache for testing purposes.

clear_cache()[source]

Clear the result cache.

get_cache_stats()[source]

Return cache statistics.

dump_cache(namespace=None)[source]

Iterate over cached entries (persistent layer only).

close()[source]

Release resources.

get_tool_health(tool_name=None)[source]

Get health status for tool(s).

check_function_call(fcall_str, function_config=None, format='llama')[source]

Validate a function call against tool configuration.

This method checks if a function call is valid by verifying the function name exists and the arguments match the expected parameters.

Parameters:
  • fcall_str – Function call string or data to validate.

  • function_config (dict, optional) – Specific function configuration to validate against. If None, uses the loaded tool configuration.

  • format (str, optional) – Format type for parsing. Defaults to ‘llama’.

Returns:

A tuple of (is_valid, message) where:
  • is_valid (bool): True if the function call is valid, False otherwise

  • message (str): Error message if invalid, empty if valid

Return type:

tuple

export_tool_names(output_file, category_filter=None)[source]

Export tool names to a text file (one per line).

Parameters:
  • output_file (str) – Path to the output file

  • category_filter (list, optional) – List of categories to filter by

filter_tools(include_tools=None, exclude_tools=None, include_tool_types=None, exclude_tool_types=None)[source]

Filter tools based on inclusion/exclusion criteria.

Parameters:
  • include_tools (set, optional) – Set of tool names to include

  • exclude_tools (set, optional) – Set of tool names to exclude

  • include_tool_types (set, optional) – Set of tool types to include

  • exclude_tool_types (set, optional) – Set of tool types to exclude

Returns:

Filtered list of tool configurations

Return type:

list

get_required_parameters(tool_name)[source]

Get required parameters for a specific tool.

Parameters:

tool_name (str) – Name of the tool

Returns:

List of required parameter names

Return type:

list

get_available_tools(category_filter=None, name_only=True)[source]

Get available tools, optionally filtered by category.

Parameters:
  • category_filter (list, optional) – List of categories to filter by

  • name_only (bool) – If True, return only tool names; if False, return full configs

Returns:

List of tool names or tool configurations

Return type:

list

find_tools_by_pattern(pattern, search_in='name', case_sensitive=False)[source]

Find tools matching a pattern in their name or description.

Parameters:
  • pattern (str) – Pattern to search for

  • search_in (str) – Where to search - ‘name’, ‘description’, or ‘both’

  • case_sensitive (bool) – Whether search should be case sensitive

Returns:

List of matching tool configurations

Return type:

list

get_tool_by_name(tool_names, format='default')[source]

Retrieve tool configurations by their names.

DEPRECATED: Use tool_specification() instead.

Parameters:
  • tool_names (list) – List of tool names to retrieve.

  • format (str, optional) – Output format. Options: ‘default’, ‘openai’. If ‘openai’, returns OpenAI function calling format. Defaults to ‘default’.

Returns:

List of tool configurations for the specified names.

Tools not found will be reported but not included in the result.

Return type:

list

get_tool_description(tool_name)[source]

Get the description of a tool by its name.

DEPRECATED: Use tool_specification() instead.

Parameters:

tool_name (str) – Name of the tool.

Returns:

Tool configuration if found, None otherwise.

Return type:

dict or None

remove_keys(tool_list, invalid_keys)[source]

Remove specified keys from a list of tool configurations.

DEPRECATED: Use prepare_tool_prompts(mode=’custom’, valid_keys=…) instead.

Parameters:
  • tool_list (list) – List of tool configuration dictionaries.

  • invalid_keys (list) – List of keys to remove from each tool configuration.

Returns:

Deep copy of tool list with specified keys removed.

Return type:

list

prepare_tool_examples(tool_list)[source]

Prepare tool configurations for example usage by keeping extended set of keys.

DEPRECATED: Use prepare_tool_prompts(mode=’example’) instead.

Parameters:

tool_list (list) – List of tool configuration dictionaries.

Returns:

Deep copy of tool list with only example-relevant keys.

Return type:

list

_load_tools_from_mcp_server(server_url, config)

Load tools from a specific MCP server.

discover_mcp_tools(server_urls=None, **kwargs)

Discover available tools from MCP servers without loading them.

This method connects to MCP servers to discover what tools are available without actually registering them in ToolUniverse. Useful for exploration and selective tool loading.

Parameters:

server_urlslist of str, optional

List of MCP server URLs to discover from

**kwargs

Additional options: - timeout (int): Connection timeout (default: 30) - include_schemas (bool): Include tool parameter schemas (default: True)

Returns:

dict

Discovery results with tools organized by server

Examples:

```python tu = ToolUniverse()

# Discover what’s available discovery = tu.discover_mcp_tools([

])

# Show available tools for server, info in discovery[“servers”].items():

print(f”n{server}:”) for tool in info.get(“tools”, []):

print(f” - {tool[‘name’]}: {tool[‘description’]}”)

```

list_mcp_connections()

List all active MCP connections and loaded tools.

Returns:

dict

Information about MCP connections, auto-loaders, and loaded tools

Examples:

```python tu = ToolUniverse() tu.load_mcp_tools([”http://localhost:8001”])

connections = tu.list_mcp_connections() print(f”Active MCP connections: {len(connections[‘connections’])}”) ```

load_mcp_tools(server_urls=None, **kwargs)

Load MCP tools from remote servers into this ToolUniverse instance.

This method automatically discovers tools from MCP servers and registers them as ToolUniverse tools, enabling seamless usage of remote capabilities.

Parameters:

server_urlslist of str, optional

List of MCP server URLs to load tools from. Examples:

If None, attempts to discover from local MCP tool registry.

**kwargs

Additional configuration options:

  • tool_prefix (str): Prefix for loaded tool names (default: “mcp_”)

  • timeout (int): Connection timeout in seconds (default: 30)

  • auto_register (bool): Whether to auto-register discovered tools (default: True)

  • selected_tools (list): Specific tools to load from each server

  • categories (list): Tool categories to filter by

Returns:

dict

Summary of loaded tools with counts and any errors encountered.

Examples:

Load from specific servers: ```python tu = ToolUniverse()

# Load tools from multiple MCP servers result = tu.load_mcp_tools([

http://localhost:8001”, # Local analysis server “http://ml-server:8002”, # Remote ML server “ws://realtime:9000” # WebSocket server

])

print(f”Loaded {result[‘total_tools’]} tools from {result[‘servers_connected’]} servers”) ```

Load with custom configuration: ```python tu.load_mcp_tools(

server_urls=[”http://localhost:8001”], tool_prefix=”analysis_”, timeout=60, selected_tools=[“protein_analysis”, “drug_interaction”]

)

Auto-discovery from local registry: `python # If you have registered MCP tools locally, auto-discover their servers tu.load_mcp_tools()  # Uses servers from mcp_tool_registry `

select_tools(include_names=None, exclude_names=None, include_categories=None, exclude_categories=None)[source]

Select tools based on tool names and/or categories (tool_files keys).

DEPRECATED: Use filter_tools() instead.

Parameters:
  • include_names (list, optional) – List of tool names to include. If None, include all.

  • exclude_names (list, optional) – List of tool names to exclude.

  • include_categories (list, optional) – List of categories (tool_files keys) to include. If None, include all.

  • exclude_categories (list, optional) – List of categories (tool_files keys) to exclude.

Returns:

List of selected tool configurations.

Return type:

list

filter_tool_lists(tool_name_list, tool_desc_list, include_names=None, exclude_names=None, include_categories=None, exclude_categories=None)[source]

Directly filter tool name and description lists based on names and/or categories.

DEPRECATED: Use filter_tools() and manual list filtering instead.

Parameters:
  • tool_name_list (list) – List of tool names to filter.

  • tool_desc_list (list) – List of tool descriptions to filter (must correspond to tool_name_list).

  • include_names (list, optional) – List of tool names to include.

  • exclude_names (list, optional) – List of tool names to exclude.

  • include_categories (list, optional) – List of categories to include.

  • exclude_categories (list, optional) – List of categories to exclude.

Returns:

A tuple containing (filtered_tool_name_list, filtered_tool_desc_list).

Return type:

tuple

load_tools_from_names_list(tool_names, clear_existing=True)[source]

Load only specific tools by their names.

DEPRECATED: Use load_tools(include_tools=…) instead.

Parameters:
  • tool_names (list) – List of tool names to load

  • clear_existing (bool) – Whether to clear existing tools first

Returns:

Number of tools successfully loaded

Return type:

int