Skip to main content

A simple way to build AI agents with tools, MCP (Model Context Protocol) tools, and sub-agents.

Project description

Base Agents

A simple way to build AI agents with tools, MCP (Model Context Protocol) tools, and sub-agents

Features

  • Multiple Tool Types: Support for simple functions, MCP (Model Context Protocol) tools, and sub-agents
  • Hierarchical Agent Architecture: Create agents that can use other agents as tools
  • Async & Sync Support: Both asynchronous and synchronous execution modes
  • Easy Integration: Simple API that works with any LangChain-compatible model
  • Flexible Tool System: Mix and match different types of tools in a single agent

Installation

pip install base-agents

Quick Start

Basic Usage

from base_agents import create_agent_tool
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
import numpy as np

load_dotenv()

model = ChatOpenAI(model="gpt-4o", api_key=os.getenv("OPENAI_API_KEY"))


@tool
def complex_matrix_multiplication(
    matrix1: list[list[complex]], matrix2: list[list[complex]]
) -> list[list[complex]]:
    """
    Multiply two complex-valued matrices.

    Args:
        matrix1: First complex matrix as a 2D list (e.g., [[1+2j, 3+4j], [5+6j, 7+8j]])
        matrix2: Second complex matrix as a 2D list (same format as matrix1)

    Returns:
        The product of the two matrices as a 2D list of complex numbers

    Raises:
        ValueError: If matrices cannot be multiplied due to incompatible dimensions
    """
    np_matrix1 = np.array(matrix1, dtype=complex)
    np_matrix2 = np.array(matrix2, dtype=complex)

    try:
        result = np.matmul(np_matrix1, np_matrix2)
        return result.tolist()
    except ValueError as e:
        raise ValueError(f"Matrix multiplication error: {str(e)}")


simple_tools = [complex_matrix_multiplication]

# mcp_tools = {
#     "puppeteer": {
#         "command": "npx",
#         "args": ["-y", "@modelcontextprotocol/server-puppeteer"],
#         "transport": "stdio",
#     }
# }

# sub_agent_math = [
#     create_agent_tool(
#         model,
#         simple_tools=simple_tools,
#         system_prompt="You are a subagent that can perform large number calculations.",
#     )
# ]

# Create Main Agent
agent = create_agent_tool(
    model,
    simple_tools=simple_tools,
    # mcp_tools=mcp_tools,
    # sub_agent_tools=sub_agent_math,
    system_prompt="You are a helpful assistant that can use tools and sub-agents to search online.",
)

# Example usage
response = agent.invoke(
    "Multiply the following two complex-valued matrices and return the result: Matrix A: [[1+2j, 3+4j], [5+6j, 7+8j]] Matrix B: [[2+1j, 0+3j], [1+2j, 4+0j]]"
)
print("Response:", response)

API Reference

create_agent_tool()

Creates a structured tool that wraps an agent with various capabilities.

Parameters:

Args:

  • model: A LangChain-compatible chat model (required)
  • mcp_tools: Dictionary defining MCP (Model Context Protocol) tools (optional)
  • simple_tools: List of simple function tools (optional)
  • sub_agent_tools: List of other agent tools (optional)
  • system_prompt: System prompt for the agent (required)

Raises:

  • ValueError: If all tools (mcp_tools, simple_tools, and sub_agent_tools) are None. At least one tool type must be provided.

Returns:

A StructuredTool that can be used with LangChain's agent frameworks.

Tool Types

Simple Tools

To write simple tools, use the @tool decorator from langchain_core.tools on a function, specifying input types and a docstring. The function should perform a specific task and return a result.

from langchain_core.tools import tool

@tool
def math_tool(x: float, y: float) -> float:
    """Add two numbers together"""
    return x + y


simple_tools = [math_tool]

MCP Tools

MCP tools enable integration with external services:

mcp_tools = {
    "puppeteer": {
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-puppeteer"],
        "env": {"PUPPETEER_LAUNCH_OPTIONS": '{ "headless": true }'},
        "transport": "stdio",
    },
}

Sub-agent Tools

Sub-agent tools are other agents created with create_agent_tool():

math_agent = create_agent_tool(model, simple_tools=math_tools)
main_agent = create_agent_tool(model, sub_agent_tools=[math_agent])

Usage Patterns

Synchronous Execution

response = agent.invoke("Your question here")

Asynchronous Execution

response = await agent.ainvoke("Your question here")

Concurrent Execution

import asyncio

async def run_multiple_tasks():
    tasks = [
        agent.ainvoke("Question 1"),
        agent.ainvoke("Question 2"),
        agent.ainvoke("Question 3"),
    ]
    responses = await asyncio.gather(*tasks)
    return responses

Contributing

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

License

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

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

base_agents-0.1.1.tar.gz (94.2 kB view details)

Uploaded Source

Built Distribution

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

base_agents-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file base_agents-0.1.1.tar.gz.

File metadata

  • Download URL: base_agents-0.1.1.tar.gz
  • Upload date:
  • Size: 94.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for base_agents-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f5246b00fc53cd88becd907beaa4a007ccc13ebc4c0af7dc5861bacd7b020ddf
MD5 d3880bab60bd89dad6f17c82454f05da
BLAKE2b-256 7bd0d63c4abfbaeb1618b1f7bf5e4f9848ca17762e6fa88539cb5a3a9c985ca9

See more details on using hashes here.

File details

Details for the file base_agents-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: base_agents-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for base_agents-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d7416018e9ecdf877bbfc4042d3670e0a5c90e1071b552fe9d8ab53bbafdd01
MD5 4fa40277c3c8b9729bf80fd9eda0b72f
BLAKE2b-256 facd78f049656f5552654b3ae61097857ee5b7bffb729e0e33b4389a5c98dbdc

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