agentleFS
thehumanworksthehumanworks/langchain-claude-code/.cursor/rules/claude-sdk/tools.mdc

langchain-claude-code / claude-sdk

Cursor rule1 starsChanged 12 months agoMITView on GitHub

@tool decorator, create_sdk_mcp_server for in-process custom MCP tools

The file

We split this tools.mdc into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Create custom tools that run in-process, eliminating subprocess overhead.

    line 8

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
tools.mdc
---
description: @tool decorator, create_sdk_mcp_server for in-process custom MCP tools
alwaysApply: false
---

# Custom Tools (In-Process MCP Servers)

Create custom tools that run in-process, eliminating subprocess overhead.

## Benefits Over External MCP Servers

✓ No subprocess management - runs in same process
✓ Better performance - no IPC overhead
✓ Simpler deployment - single Python process
✓ Easier debugging - all code in one process
✓ Type safety - direct Python function calls

## Creating Tools

```python
from typing import Any
from claude_agent_sdk import tool, create_sdk_mcp_server, ClaudeAgentOptions

# 1. Define tool with @tool decorator
@tool("greet", "Greet a user", {"name": str})
async def greet_user(args: dict[str, Any]) -> dict[str, Any]:
    return {
        "content": [{"type": "text", "text": f"Hello, {args['name']}!"}]
    }

# 2. Create MCP server
server = create_sdk_mcp_server(
    name="my-tools",
    version="1.0.0",
    tools=[greet_user]
)

# 3. Configure options (CRITICAL: add to allowed_tools)
options = ClaudeAgentOptions(
    mcp_servers={"tools": server},
    allowed_tools=["mcp__tools__greet"]  # mcp__{server}__{tool}
)
```

## Tool Naming Convention

**Pattern**: `mcp__{server_name}__{tool_name}`

```python
# Server named "calc" with tool named "add"
allowed_tools=["mcp__calc__add"]

# Server named "my-tools" with tool named "greet"
allowed_tools=["mcp__my-tools__greet"]
```

## Complete Calculator Example

```python
from typing import Any
from claude_agent_sdk import (
    tool,
    create_sdk_mcp_server,
    ClaudeAgentOptions,
    ClaudeSDKClient,
)

@tool("add", "Add two numbers", {"a": float, "b": float})
async def add_numbers(args: dict[str, Any]) -> dict[str, Any]:
    result = args["a"] + args["b"]
    return {
        "content": [{"type": "text", "text": f"{args['a']} + {args['b']} = {result}"}]
    }

@tool("divide", "Divide two numbers", {"a": float, "b": float})
async def divide_numbers(args: dict[str, Any]) -> dict[str, Any]:
    if args["b"] == 0:
        return {
            "content": [{"type": "text", "text": "Error: Division by zero"}],
            "is_error": True,
        }
    result = args["a"] / args["b"]
    return {
        "content": [{"type": "text", "text": f"{args['a']} ÷ {args['b']} = {result}"}]
    }

calculator = create_sdk_mcp_server(
    name="calc",
    version="1.0.0",
    tools=[add_numbers, divide_numbers],
)

options = ClaudeAgentOptions(
    mcp_servers={"calc": calculator},
    allowed_tools=["mcp__calc__add", "mcp__calc__divide"],
)

async with ClaudeSDKClient(options=options) as client:
    await client.query("Calculate 15 + 27")
    async for msg in client.receive_response():
        print(msg)
```

## Tool Return Format

Tools return a dict with `content` array:

```python
# Success
return {
    "content": [{"type": "text", "text": "Result message"}]
}

# Error
return {
    "content": [{"type": "text", "text": "Error description"}],
    "is_error": True,
}
```

## Mixed Server Support

Combine SDK (in-process) and external (subprocess) MCP servers:

```python
options = ClaudeAgentOptions(
    mcp_servers={
        "internal": sdk_server,       # In-process SDK server
        "external": {                  # External subprocess server
            "type": "stdio",
            "command": "external-server"
        }
    }
)
```

## Migration from External Servers

```python
# BEFORE: External MCP server (separate process)
options = ClaudeAgentOptions(
    mcp_servers={
        "calculator": {
            "type": "stdio",
            "command": "python",
            "args": ["-m", "calculator_server"]
        }
    }
)

# AFTER: SDK MCP server (in-process)
calculator = create_sdk_mcp_server(
    name="calculator",
    tools=[add, subtract]
)
options = ClaudeAgentOptions(
    mcp_servers={"calculator": calculator}
)
```

## Pitfall: Missing allowed_tools

Registering a server is insufficient. Tools must be explicitly allowed:

```python
# ✗ WRONG: Tool registered but not allowed
options = ClaudeAgentOptions(
    mcp_servers={"tools": server},
    # Missing allowed_tools!
)

# ✓ CORRECT: Tool registered AND allowed
options = ClaudeAgentOptions(
    mcp_servers={"tools": server},
    allowed_tools=["mcp__tools__greet"]
)
```

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.