Skip to main content

Training-free defense for multi-turn safety risks in tool-using AI agents

Project description

ToolShield: Training-Free Defense for Multi-Turn Safety Risks in Tool-Using AI Agents

PyPI Python License Homepage HuggingFace

Supports:ย  Claude Code Codex Cursor OpenHands OpenClaw


Quickstart | Pre-generated | Generate Your Own | Extend | Benchmark | Citation

ToolShield is a training-free, tool-agnostic defense for AI agents that use MCP tools. It works by letting the agent explore tool functionality in a sandbox, learning from its own executions, and distilling safety guidelines before deployment. Reduces attack success rate by 30% on average โ€” with zero fine-tuning.

Overview

Quickstart

pip install toolshield

Use Pre-generated Experiences

We ship safety experiences for 6 models across 5 tools, with plug-and-play support for 5 coding agents. Inject them in one command:

# For Claude Code
toolshield import \
  --exp-file experiences/claude-sonnet-4.5/experience_list_claude-sonnet-4.5_postgres.json \
  --agent claude_code

# For Codex
toolshield import \
  --exp-file experiences/claude-sonnet-4.5/experience_list_claude-sonnet-4.5_postgres.json \
  --agent codex

# For OpenClaw
toolshield import \
  --exp-file experiences/claude-sonnet-4.5/experience_list_claude-sonnet-4.5_postgres.json \
  --agent openclaw

# For Cursor (writes to global user rules via SQLite)
toolshield import \
  --exp-file experiences/claude-sonnet-4.5/experience_list_claude-sonnet-4.5_postgres.json \
  --agent cursor

# For OpenHands (creates a microagent)
toolshield import \
  --exp-file experiences/claude-sonnet-4.5/experience_list_claude-sonnet-4.5_postgres.json \
  --agent openhands

This appends safety guidelines to your agent's context file (~/.claude/CLAUDE.md, ~/.codex/AGENTS.md, ~/.openclaw/workspace/AGENTS.md, Cursor's global user rules, or ~/.openhands/microagents/toolshield.md). To remove them:

toolshield unload --agent claude_code

Available experiences in experiences/:

Model ๐Ÿ“ Filesystem ๐Ÿ˜ PostgreSQL ๐Ÿ’ป Terminal ๐ŸŽญ Playwright ๐Ÿ“ Notion
claude-sonnet-4.5 โœ… โœ… โœ… โœ… โœ…
gpt-5.2 โœ… โœ… โœ… โœ… โœ…
deepseek-v3.2 โœ… โœ… โœ… โœ… โœ…
gemini-3-flash-preview โœ… โœ… โœ… โœ… โœ…
qwen3-coder-plus โœ… โœ… โœ… โœ… โœ…
seed-1.6 โœ… โœ… โœ… โœ… โœ…

More plug-and-play experiences for additional tools coming soon (including Toolathlon support)! Have a tool you'd like covered? Open an issue.

Generate Your Own

Point ToolShield at any running MCP server to generate custom safety experiences:

export TOOLSHIELD_MODEL_NAME="anthropic/claude-sonnet-4.5"
export OPENROUTER_API_KEY="your-key"

# Full pipeline: inspect โ†’ generate safety tree โ†’ test โ†’ distill โ†’ inject
toolshield \
  --mcp_name postgres \
  --mcp_server http://localhost:9091 \
  --output_path output/postgres \
  --agent codex

Or generate without injecting (useful for review):

toolshield generate \
  --mcp_name postgres \
  --mcp_server http://localhost:9091 \
  --output_path output/postgres

Extend to New Tools

ToolShield works with any MCP server that has an SSE endpoint:

toolshield generate \
  --mcp_name your_custom_tool \
  --mcp_server http://localhost:PORT \
  --output_path output/your_custom_tool

MT-AgentRisk Benchmark

We also release MT-AgentRisk, a benchmark of 365 harmful tasks across 5 MCP tools, transformed into multi-turn attack sequences. See agentrisk/README.md for full evaluation setup.

Quick evaluation:

# 1. Download benchmark tasks
git clone https://huggingface.co/datasets/CHATS-Lab/MT-AgentRisk
cp -r MT-AgentRisk/workspaces/* workspaces/

# 2. Run a single task (requires OpenHands setup โ€” see agentrisk/README.md)
python agentrisk/run_eval.py \
  --task-path workspaces/terminal/multi_turn_tasks/multi-turn_root-remove \
  --agent-llm-config agent \
  --env-llm-config env \
  --outputs-path output/eval \
  --server-hostname localhost

Add --use-experience <path> to evaluate with ToolShield defense.

Repository Layout

ToolShield/
โ”œโ”€โ”€ toolshield/          # pip-installable defense package
โ”œโ”€โ”€ agentrisk/           # evaluation framework (see agentrisk/README.md)
โ”œโ”€โ”€ experiences/         # pre-generated safety experiences (6 models ร— 5 tools)
โ”œโ”€โ”€ workspaces/          # MT-AgentRisk task data (from HuggingFace)
โ”œโ”€โ”€ docker/              # Dockerfiles and compose
โ””โ”€โ”€ scripts/             # experiment reproduction guides

Acknowledgments

We thank the authors of the following projects for their contributions:

Citation

License

MIT

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

toolshield-0.1.0.tar.gz (3.8 MB view details)

Uploaded Source

Built Distribution

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

toolshield-0.1.0-py3-none-any.whl (57.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: toolshield-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for toolshield-0.1.0.tar.gz
Algorithm Hash digest
SHA256 271946318233bfdedda0eb73faa00c4b7f118696ec0ca56450d7d9ef84fb22ce
MD5 de059bc256c5f944e281d2501ef2e4aa
BLAKE2b-256 db4558b5abb3ae764ccad0aa5076bc30145062209140642ed923323e1357fb94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: toolshield-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 57.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for toolshield-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50bf760537ac95ea4609460a6438a2e8c43593516c36bb66a6e43393e06f1798
MD5 d671b972451baa7ab3182b757b9712fa
BLAKE2b-256 c94543b99fc7ed47fadb1dac53d7b7a19e003917af6fb5e0b25c2eed5e5d89f6

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