Toolkits Module¶
The cogent.toolkits module provides composable tools that plug into any agent. Toolkits are reusable building blocks that add domain-specific functionality.
Overview¶
Toolkits encapsulate related functionality and expose it through native tools. Each toolkit: - Provides tools for the agent to use - May maintain internal state (graphs, caches, connections) - Can be initialized/shutdown with the agent lifecycle
from cogent import Agent
from cogent.toolkits import (
KnowledgeGraph, FileSystem, WebSearch, CodeSandbox,
MCP, Spreadsheet, PDF, Browser, Shell, Summarizer,
)
agent = Agent(
name="Assistant",
model=model,
tools=[
KnowledgeGraph(), # Entity/relationship memory
FileSystem(allowed_paths=["./data"]), # Sandboxed file operations
WebSearch(), # Web search and fetching
CodeSandbox(), # Safe Python execution
],
)
Available Toolkits¶
KnowledgeGraph¶
Entity/relationship memory with multi-hop reasoning and multiple storage backends.
from cogent.toolkits import KnowledgeGraph
# In-memory (default, no persistence)
kg = KnowledgeGraph()
# In-memory with auto-save to file
kg = KnowledgeGraph(backend="memory", path="memory.json", auto_save=True)
# SQLite for persistence
kg = KnowledgeGraph(backend="sqlite", path="knowledge.db")
# JSON file with auto-save
kg = KnowledgeGraph(backend="json", path="knowledge.json")
# Custom backend instance
from cogent.toolkits.knowledge_graph.backends import GraphBackend
custom_backend = MyCustomBackend() # Your implementation
kg = KnowledgeGraph(backend=custom_backend)
agent = Agent(
name="Researcher",
model=model,
tools=[kg],
)
Backend Switching:
Switch backends dynamically with optional data migration:
# Start with in-memory
kg = KnowledgeGraph()
kg.remember("Alice", "Person", {"role": "Engineer"})
# Switch to SQLite with migration
kg.set_backend("sqlite", path="knowledge.db", migrate=True)
# All data is now persisted, continue using same instance
kg.remember("Bob", "Person", {"role": "Manager"})
# Switch to JSON
kg.set_backend("json", path="knowledge.json", migrate=True)
Tools provided:
| Tool | Description |
|------|-------------|
| remember | Store entities with attributes (dict or JSON string) |
| recall | Retrieve information about entities |
| connect | Create relationships between entities |
| query_knowledge | Query relationships (source/relation/target params) |
| forget | Remove entities and their relationships |
| list_knowledge | List all entities, optionally filtered by type |
Backends:
- memory: Fast in-memory (uses networkx if available) - optionally auto-save to file
- sqlite: Persistent SQLite for large graphs - always saves
- json: Simple JSON file with auto-save - always saves
- neo4j: Production graph database (requires neo4j package) - always saves
- Custom: Extend GraphBackend for your own implementation
Visualization:
kg = KnowledgeGraph()
# ... add entities and relationships ...
# Raw Mermaid code
code = kg.mermaid(direction="LR")
# Render + save — format inferred from extension
kg.render(direction="LR", output_path="graph.mmd") # Mermaid source
kg.render(direction="LR", output_path="graph.svg") # SVG via kroki.io
kg.render(direction="LR", output_path="graph.png") # PNG via kroki.io
kg.render(direction="LR", output_path="graph.pdf") # PDF via kroki.io
# Use mermaid.ink as alternative backend (SVG/PNG only)
kg.render(output_path="graph.svg", renderer="ink")
# Interactive web visualization (requires gravis + networkx)
fig = kg.visualize(output_path="graph.html")
Image rendering uses hosted APIs (kroki.io by default, mermaid.ink as alternative) — no local tooling required.
Visualization options:
- direction: Layout direction — "LR" (left-right), "TB" (top-bottom), "BT", "RL"
- group_by_type: Group entities by type in subgraphs
- renderer: "kroki" (default, SVG/PNG/PDF) or "ink" (SVG/PNG only)
Entity colors:
- Person: Blue (#90CAF9)
- Company/Organization: Green (#A5D6A7)
- Location: Orange (#FFCC80)
- Event: Purple (#CE93D8)
- Generic: Gray (#f0f0f0)
Tool API (improved in v1.8.3):
# Query with structured parameters (NEW)
query_knowledge(source=None, relation="works_at", target="TechCorp")
# Returns: Who works at TechCorp?
# Remember with dict attributes (NEW - preferred)
remember(entity="Alice", entity_type="Person", attributes={"role": "CEO", "age": 35})
# Also accepts JSON string for backward compatibility
remember(entity="Alice", entity_type="Person", attributes='{"role": "CEO"}')
FileSystem¶
Sandboxed file operations with security controls.
from cogent.toolkits import FileSystem
# Read-only access to docs
fs = FileSystem(
allowed_paths=["./docs"],
allow_write=False,
)
# Full access with security
fs = FileSystem(
allowed_paths=["./workspace"],
deny_patterns=["*.env", "*.key", ".git/*"],
allow_delete=True,
max_file_size=10 * 1024 * 1024, # 10MB
)
agent = Agent(name="Worker", model=model, tools=[fs])
Tools provided:
| Tool | Description |
|------|-------------|
| read_file | Read file contents |
| write_file | Write/update files |
| list_directory | List directory contents |
| search_files | Search by pattern/content |
| copy_file | Copy files |
| move_file | Move/rename files |
| delete_file | Delete files (if enabled) |
Parameters:
- allowed_paths: Directories the agent can access
- deny_patterns: Glob patterns to block (e.g., ["*.env", ".git/*"])
- max_file_size: Maximum file size in bytes (default: 10MB)
- allow_write: Enable write operations (default: True)
- allow_delete: Enable delete operations (default: False)
WebSearch¶
Web search and page fetching using DuckDuckGo (free, no API key).
from cogent.toolkits import WebSearch
# Default configuration
ws = WebSearch()
# Custom settings
ws = WebSearch(
max_results=10,
timeout=30,
include_raw_html=False,
)
agent = Agent(name="Researcher", model=model, tools=[ws])
Tools provided:
| Tool | Description |
|------|-------------|
| web_search | Search the web |
| news_search | Search news articles |
| fetch_page | Fetch and extract page content |
| fetch_multiple | Fetch multiple URLs concurrently |
Requires: uv add ddgs
CodeSandbox¶
Safe Python code execution with security controls.
from cogent.toolkits import CodeSandbox
sandbox = CodeSandbox(
timeout=30, # Execution timeout in seconds
max_output_size=10000, # Max output characters
allow_imports=["math", "json", "re"], # Allowed imports
)
agent = Agent(name="Coder", model=model, tools=[sandbox])
Tools provided:
| Tool | Description |
|------|-------------|
| execute_python | Run Python code |
| run_function | Execute a specific function |
| eval_expression | Evaluate an expression |
Security features: - Blocked dangerous imports (os, subprocess, socket, etc.) - Resource limits (time, memory) - AST-based code analysis - Sandboxed execution environment
MCP (Model Context Protocol)¶
Connect to local and remote MCP servers. MCP is also available as a dedicated
mcps= argument on Agent for protocol-level external connections.
from cogent.toolkits import MCP
# Local server (stdio)
mcp_local = MCP.stdio(
command="uv",
args=["run", "my-mcp-server"],
)
# Remote server (HTTP/SSE)
mcp_http = MCP.http("https://api.example.com/mcp")
# WebSocket
mcp_ws = MCP.websocket("ws://localhost:8766")
# First-class mcps= arg — accepts a single MCP instance or a list
agent = Agent(
name="Assistant",
model=model,
mcps=MCP.http("https://tools.example.com/mcp"),
)
# Multiple MCP servers of different transports
agent = Agent(
name="Assistant",
model=model,
mcps=[
MCP.stdio(command="npx", args=["-y", "@modelcontextprotocol/server-filesystem", "."]),
MCP.http("https://weather-api.example.com/mcp"),
],
)
# MCP instances can also go in tools=
agent = Agent(
name="Assistant",
model=model,
tools=[
MCP.stdio(command="npx", args=["-y", "@modelcontextprotocol/server-filesystem", "."]),
MCP.http("https://weather-api.example.com/mcp"),
],
)
Transport types:
- STDIO: Local process communication
- HTTP: HTTP/Streamable HTTP
- SSE: Server-Sent Events
- WEBSOCKET: WebSocket connection
Spreadsheet¶
Excel and CSV file operations.
from cogent.toolkits import Spreadsheet
ss = Spreadsheet(
default_format="xlsx",
max_rows=100000,
)
agent = Agent(name="Analyst", model=model, tools=[ss])
Tools provided:
| Tool | Description |
|------|-------------|
| read_spreadsheet | Read Excel/CSV files |
| write_spreadsheet | Create/update spreadsheets |
| query_spreadsheet | Query with filters |
| spreadsheet_stats | Get statistics |
Requires: uv add openpyxl pandas
Browser¶
Headless browser automation with Playwright.
from cogent.toolkits import Browser
browser = Browser(
headless=True,
timeout=30000,
)
agent = Agent(name="WebAgent", model=model, tools=[browser])
Tools provided:
| Tool | Description |
|------|-------------|
| browse_url | Navigate to URL |
| click_element | Click on elements |
| fill_form | Fill form fields |
| screenshot | Take screenshots |
| extract_content | Extract page content |
Requires: uv add playwright && playwright install
Shell¶
Sandboxed terminal command execution.
from cogent.toolkits import Shell
shell = Shell(
allowed_commands=["ls", "cat", "grep", "find"],
working_dir="./workspace",
timeout=30,
)
agent = Agent(name="SysAdmin", model=model, tools=[shell])
Tools provided:
| Tool | Description |
|------|-------------|
| run_command | Execute shell command |
| run_script | Execute shell script |
Security features: - Command allowlist/blocklist - Working directory sandboxing - Timeout enforcement - Output size limits
Summarizer¶
Document summarization with multiple strategies.
from cogent.toolkits import Summarizer, SummarizerConfig
summarizer = Summarizer(
config=SummarizerConfig(
strategy="map_reduce", # or "refine", "hierarchical"
chunk_size=4000,
max_concurrency=5,
),
)
agent = Agent(name="Summarizer", model=model, tools=[summarizer])
Tools provided:
| Tool | Description |
|------|-------------|
| summarize_text | Summarize text content |
| summarize_file | Summarize file contents |
| summarize_url | Fetch and summarize URL |
Strategies:
- map_reduce: Parallel chunk summarization + combine
- refine: Iterative refinement through chunks
- hierarchical: Multi-level summarization tree
CodebaseAnalyzer¶
Python codebase analysis with AST parsing.
from cogent.toolkits import CodebaseAnalyzer
analyzer = CodebaseAnalyzer(
root_path="./src",
include_patterns=["*.py"],
)
agent = Agent(name="CodeReviewer", model=model, tools=[analyzer])
Tools provided:
| Tool | Description |
|------|-------------|
| analyze_file | Analyze Python file structure |
| find_definitions | Find classes/functions |
| find_usages | Find symbol usages |
| get_dependencies | Analyze imports |
Creating Custom Toolkits¶
Extend Toolkit to create your own:
from cogent.toolkits.base import Toolkit
from cogent.tools import tool, BaseTool
class MyToolkit(Toolkit):
@property
def name(self) -> str:
return "my_toolkit"
@property
def description(self) -> str:
return "Does something useful"
@property
def tools(self) -> list[BaseTool]:
return [self._my_tool()]
def _my_tool(self):
@tool
def do_something(x: str) -> str:
'''Do something useful.'''
return f"Did: {x}"
return do_something
async def initialize(self, agent) -> None:
"""Called when attached to agent."""
pass
async def shutdown(self) -> None:
"""Called when agent shuts down."""
pass