MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / main

Function main

examples/managed-agents-worker-dispatch.py:48–69  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

46
47
48async def main() -> None:
49 logging.basicConfig(level=logging.INFO)
50 client = AsyncAnthropic()
51
52 # No agent/session creation and no polling here — an upstream poller already
53 # claimed the item. Build the worker with just a `tools` factory: it is
54 # invoked once per claimed session with that session's `AgentToolContext`,
55 # so the built-in toolset binds to the right per-session workdir.
56 # (`worker(...)` returns an `EnvironmentWorker`; you can also construct one
57 # directly with `EnvironmentWorker(client, ...)` from `anthropic.lib.environments`.)
58 worker = client.beta.environments.work.worker(
59 workdir="/workspace",
60 tools=lambda env: [*beta_agent_toolset_20260401(env), current_time],
61 )
62
63 # handle_item() with no arguments reads ANTHROPIC_WORK_ID /
64 # ANTHROPIC_ENVIRONMENT_ID / ANTHROPIC_SESSION_ID / ANTHROPIC_ENVIRONMENT_KEY
65 # from the environment, then runs the per-item flow: set up the workdir +
66 # skills, run the session's tool calls while heartbeating the lease, and
67 # force-stop the work item on exit. It returns once the item is done.
68 await worker.handle_item()
69 print("work item complete")
70
71
72if __name__ == "__main__":

Calls 4

AsyncAnthropicClass · 0.90
handle_itemMethod · 0.80
workerMethod · 0.45

Tested by

no test coverage detected