Skip to content

Open-source Python SDK that enables developers to discover, connect, and control AI agents registered in the NANDA Index or running on Model Context Protocol (MCP) servers.

Notifications You must be signed in to change notification settings

code4businesss/cortex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 CORTEX β€” Python SDK for NANDA & MCP Agents

CORTEX is an open-source Python SDK that enables developers to discover, connect, and control AI agents registered in the NANDA Index or running on Model Context Protocol (MCP) servers.

It provides a unified interface to interact with intelligent agents β€” allowing developers to invoke tools, access resources, and enforce policies β€” all while maintaining strict control over what each agent can access, execute, or disclose.


πŸš€ Key Features

  • πŸ”— NANDA-ready: Connect to any agent by its NANDA_ID, automatically resolving endpoints and protocols.
  • βš™οΈ MCP-compatible: Interact with tools and resources exposed via the Model Context Protocol (MCP).
  • 🧩 Policy Engine: Define allow/deny lists, scopes, redaction filters, and rate limits to govern agent behavior.
  • 🧠 Context-aware: Manage runtime access boundaries and agent visibility dynamically.
  • πŸ”’ Secure by Design: TLS/mTLS support, PII redaction, and audit logging.
  • πŸ“Š Observability: Built-in OpenTelemetry tracing and structured logging.
  • 🧰 Developer-friendly: Sync & async APIs, CLI tools, and YAML-based configuration.

🧭 Why CORTEX?

In the emerging Internet of Agents, CORTEX acts as the control layer between humans, models, and tools β€” ensuring every interaction is traceable, governed, and context-aware.

By bridging NANDA's discovery layer and MCP's execution layer, CORTEX empowers developers to safely compose multi-agent systems that are modular, compliant, and production-ready.


βš™οΈ Installation

pip install cortex-nanda

For observability features (OpenTelemetry):

pip install cortex-nanda[observability]

πŸ“– Quick Start

1. Initialize Configuration

cortex init-config --output cortex.yaml

2. Configure MCP Server

Edit cortex.yaml:

mcp_servers:
  my_server:
    command: "npx"
    args: ["-y", "@modelcontextprotocol/server-filesystem"]
    transport: "stdio"

policies:
  default:
    allowed_tools:
      - "read_file"
      - "list_directory"
    denied_tools:
      - ".*delete.*"
    rate_limit_per_minute: 60
    rate_limit_per_hour: 1000
    redaction_patterns:
      - "\\b\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}\\b"  # Credit cards

3. Use the SDK

Sync API

from cortex import CortexClient, CortexConfig

# Load configuration
config = CortexConfig.from_yaml("cortex.yaml")

# Connect to MCP server
with CortexClient(config=config, server_name="my_server") as client:
    # List available tools
    tools = client.list_tools()
    print(f"Available tools: {[t['name'] for t in tools]}")
    
    # Call a tool
    result = client.call_tool("read_file", {"path": "example.txt"})
    print(result)
    
    # List resources
    resources = client.list_resources()
    
    # Read a resource
    content = client.read_resource("file:///path/to/file.txt")

Async API

import asyncio
from cortex import AsyncCortexClient, CortexConfig

async def main():
    config = CortexConfig.from_yaml("cortex.yaml")
    
    async with AsyncCortexClient(config=config, server_name="my_server") as client:
        tools = await client.list_tools()
        result = await client.call_tool("read_file", {"path": "example.txt"})

asyncio.run(main())

Custom Policy

from cortex import CortexClient, Policy

# Define a custom policy
policy = Policy(
    allowed_tools={"read_file", "list_directory"},
    denied_tools={"delete_file", "write_file"},
    rate_limit_per_minute=30,
    redaction_patterns=[
        r"\b\d{3}-\d{2}-\d{4}\b",  # SSN
    ],
)

# Use with client
client = CortexClient(policy=policy)
client.connect("my_server")

πŸ› οΈ CLI Usage

# List available tools
cortex --server my_server --config cortex.yaml list-tools

# Call a tool
cortex --server my_server --config cortex.yaml call-tool read_file --args path=example.txt

# List resources
cortex --server my_server --config cortex.yaml list-resources

# Read a resource
cortex --server my_server --config cortex.yaml read-resource file:///path/to/file.txt

# View audit log
cortex --server my_server --config cortex.yaml audit-log

🧩 Policy Engine

The policy engine enforces:

  • Allow/Deny Lists: Control which tools and resources can be accessed
  • Rate Limiting: Prevent abuse with per-minute and per-hour limits
  • PII Redaction: Automatically redact sensitive information using regex patterns
  • Context Size Limits: Control the size of context passed to agents
  • Custom Validators: Implement custom validation logic
  • Audit Logging: Track all policy decisions

Policy Configuration

from cortex import Policy

policy = Policy(
    # Tool access control
    allowed_tools={"read_file", "list_directory"},
    denied_tools={".*delete.*", ".*write.*"},
    
    # Resource access control
    allowed_resources={"file:///safe/.*"},
    denied_resources={"file:///private/.*"},
    
    # Rate limiting
    rate_limit_per_minute=60,
    rate_limit_per_hour=1000,
    
    # PII redaction
    redaction_patterns=[
        r"\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b",  # Credit cards
        r"\b\d{3}-\d{2}-\d{4}\b",  # SSN
        r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b",  # Email
    ],
    
    # Context limits
    max_context_size=100000,  # bytes
    
    # Custom validation
    custom_validator=lambda resource, context: "admin" in context.get("user", {}).get("roles", []),
)

πŸ“Š Observability

CORTEX includes built-in observability features:

Logging

import logging
from cortex.observability import setup_observability

# Setup observability
obs = setup_observability(
    enable_logging=True,
    enable_tracing=True,
    log_level="INFO",
)

# Log events
obs.log_event("INFO", "Tool executed", {"tool": "read_file", "path": "example.txt"})

OpenTelemetry Tracing

from cortex.observability import ObservabilityManager

obs = ObservabilityManager(enable_tracing=True)

@obs.trace_operation("call_tool")
def call_tool(tool_name, args):
    # Your code here
    pass

πŸ”’ Security Features

  • TLS/mTLS Support: Secure connections to MCP servers
  • PII Redaction: Automatic redaction of sensitive data
  • Audit Logging: Complete audit trail of all operations
  • Rate Limiting: Prevent abuse and DoS attacks
  • Access Control: Fine-grained allow/deny lists

πŸ“ Configuration Reference

See cortex.yaml example:

mcp_servers:
  filesystem:
    command: "npx"
    args: ["-y", "@modelcontextprotocol/server-filesystem"]
    env:
      MCP_SERVER_ROOT: "/path/to/root"
    transport: "stdio"

policies:
  default:
    allowed_tools: []
    denied_tools: []
    allowed_resources: []
    denied_resources: []
    scopes: []
    rate_limit_per_minute: 60
    rate_limit_per_hour: 1000
    redaction_patterns: []
    max_context_size: null
    require_authentication: true
  
  servers:
    filesystem:
      allowed_tools:
        - "read_file"
        - "list_directory"
      denied_tools:
        - ".*delete.*"
      rate_limit_per_minute: 30

tls:
  enabled: false
  cert_path: null
  key_path: null
  ca_path: null

observability:
  tracing: true
  logging: true
  log_level: "INFO"

🀝 Contributing

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


πŸ“„ License

MIT License


πŸ”— Links


Built with ❀️ for the Internet of Agents

About

Open-source Python SDK that enables developers to discover, connect, and control AI agents registered in the NANDA Index or running on Model Context Protocol (MCP) servers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages