Aan de slagBegin gratis

Grant the SDK a tool

By default, the Agent SDK is read-only: Claude can read and search, but it cannot change your files. To let it make edits, you create a ClaudeAgentOptions object listing the tools you want to allow and pass it to query().

A package.json is in your working directory. Complete the script so the SDK is allowed to use the Edit tool and asks Claude to add a description. The script prints package.json before and after, so you can watch the edit happen.

Deze oefening maakt deel uit van de cursus

Claude Code in Action

Bekijk cursus

Oefeninstructies

  • Create options by calling ClaudeAgentOptions() with allowed_tools set to a list containing "Edit".
  • Pass your options object to query() using the options argument.

Interactieve oefening met praktijkervaring

Probeer deze oefening door deze voorbeeldcode aan te vullen.

import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

# package.json before the SDK runs
print("BEFORE:\n" + open("package.json").read())


async def main():
    # The SDK is read-only by default; allow the Edit tool so Claude can write
    options = ____

    # Pass the options so the permission takes effect
    async for message in query(
        prompt='Add a "description" field to package.json.',
        options=____,
    ):
        print(message)


asyncio.run(main())

# package.json after the SDK runs - the description now appears
print("AFTER:\n" + open("package.json").read())
Code bewerken en uitvoeren