Skip to content

can_use_tool does not work when serializing PermissionResultAllow #200

@Ran4

Description

@Ran4

Running
claude-agent-sdk
version = "0.1.0"

Example standalone code:

import asyncio
from pprint import pprint

from claude_agent_sdk import (
    ClaudeSDKClient,
    ClaudeAgentOptions,
    ContentBlock,
    Message,
    PermissionResultAllow,
    PermissionResultDeny,
    ToolPermissionContext,
    create_sdk_mcp_server,
)
from anthropic.types.message_param import MessageParam


async def custom_permission_handler(tool_name: str, input_data: dict, context: dict):
    print("UNSECURE AUTO ALLOW TOOL", tool_name, input_data)
    return PermissionResultAllow()

async def permission_example():
    options = ClaudeAgentOptions(
        max_turns=10,
        allowed_tools=["Read", "Grep", "Glob", "Edit"],
        can_use_tool=custom_permission_handler,
    )

    async with ClaudeSDKClient(options) as client:
        await client.query("Create foo.txt containing the digits 1-10")

        async for message in client.receive_response():
            pprint(message.__dict__)
            print()


if __name__ == "__main__":
    asyncio.run(permission_example())

When this is run, the final output is:

EXAMPLE UNSECURE ALLOWS ALLOW TOOL Write {'file_path': '/home/ran/it_total/ttyd/backend-ttyd/foo.txt', 'content': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n'}
{'content': [ToolResultBlock(tool_use_id='toolu_01GQJijiuzmj7pR1UNV7sQhK',
                             content='Tool permission request failed: [\n'
                                     '  {\n'
                                     '    "code": "invalid_union",\n'
                                     '    "unionErrors": [\n'
                                     '      {\n'
                                     '        "issues": [\n'
                                     '          {\n'
                                     '            "code": "invalid_literal",\n'
                                     '            "expected": "allow",\n'
                                     '            "path": [\n'
                                     '              "behavior"\n'
                                     '            ],\n'
                                     '            "message": "Invalid literal '
                                     'value, expected \\"allow\\""\n'
                                     '          },\n'
                                     '          {\n'
                                     '            "code": "invalid_type",\n'
                                     '            "expected": "object",\n'
                                     '            "received": "undefined",\n'
                                     '            "path": [\n'
                                     '              "updatedInput"\n'
                                     '            ],\n'
                                     '            "message": "Required"\n'
                                     '          }\n'
                                     '        ],\n'
                                     '        "name": "ZodError"\n'
                                     '      },\n'
                                     '      {\n'
                                     '        "issues": [\n'
                                     '          {\n'
                                     '            "code": "invalid_literal",\n'
                                     '            "expected": "deny",\n'
                                     '            "path": [\n'
                                     '              "behavior"\n'
                                     '            ],\n'
                                     '            "message": "Invalid literal '
                                     'value, expected \\"deny\\""\n'
                                     '          },\n'
                                     '          {\n'
                                     '            "code": "invalid_type",\n'
                                     '            "expected": "string",\n'
                                     '            "received": "undefined",\n'
                                     '            "path": [\n'
                                     '              "message"\n'
                                     '            ],\n'
                                     '            "message": "Required"\n'
                                     '          }\n'
                                     '        ],\n'
                                     '        "name": "ZodError"\n'
                                     '      }\n'
                                     '    ],\n'
                                     '    "path": [],\n'
                                     '    "message": "Invalid input"\n'
                                     '  }\n'
                                     ']',
                             is_error=True)],
 'parent_tool_use_id': None}

{'content': [TextBlock(text='\n'
                            '\n'
                            "I'm encountering an issue with the tool "
                            'permission system. This appears to be a hook '
                            'configuration issue. Could you check your hooks '
                            'configuration? In the meantime, you can create '
                            'the file manually using:\n'
                            '\n'
                            '```bash\n'
                            'echo -e "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10" '
                            '> foo.txt\n'
                            '```\n'
                            '\n'
                            'Or using a simple loop:\n'
                            '```bash\n'
                            'seq 1 10 > foo.txt\n'
                            '```')],
 'model': 'claude-sonnet-4-5-20250929',
 'parent_tool_use_id': None}

{'duration_api_ms': 9553,
 'duration_ms': 9645,
 'is_error': False,
 'num_turns': 6,
 'result': '\n'
           '\n'
           "I'm encountering an issue with the tool permission system. This "
           'appears to be a hook configuration issue. Could you check your '
           'hooks configuration? In the meantime, you can create the file '
           'manually using:\n'
           '\n'
           '```bash\n'
           'echo -e "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10" > foo.txt\n'
           '```\n'
           '\n'
           'Or using a simple loop:\n'
           '```bash\n'
           'seq 1 10 > foo.txt\n'
           '```',
 'session_id': 'a0ccda59-a10e-4caa-936e-04368afb9e67',
 'subtype': 'success',
 'total_cost_usd': 0.0188055,
 'usage': {'cache_creation': {'ephemeral_1h_input_tokens': 0,
                              'ephemeral_5m_input_tokens': 0},
           'cache_creation_input_tokens': 0,
           'cache_read_input_tokens': 45375,
           'input_tokens': 16,
           'output_tokens': 343,
           'server_tool_use': {'web_search_requests': 0},
           'service_tier': 'standard'}}

Essentially, returning PermissionResultAllow fails during serialization.

This is the content:

Tool permission request failed: [
  {
    "code": "invalid_union",
    "unionErrors": [
      {
        "issues": [
          {
            "code": "invalid_literal",
            "expected": "allow",
            "path": [
              "behavior"
            ],
            "message": "Invalid literal value, expected \"allow\""
          },
          {
            "code": "invalid_type",
            "expected": "object",
            "received": "undefined",
            "path": [
              "updatedInput"
            ],
            "message": "Required"
          }
        ],
        "name": "ZodError"
      },
      {
        "issues": [
          {
            "code": "invalid_literal",
            "expected": "deny",
            "path": [
              "behavior"
            ],
            "message": "Invalid literal value, expected \"deny\""
          },
          {
            "code": "invalid_type",
            "expected": "string",
            "received": "undefined",
            "path": [
              "message"
            ],
            "message": "Required"
          }
        ],
        "name": "ZodError"
      }
    ],
    "path": [],
    "message": "Invalid input"
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions