Skip to main content

Lightweight Python SDK for building AI agents.

Project description

mycode-sdk

Lightweight Python SDK for building AI agents.

Install

uv add mycode-sdk
# or
pip install mycode-sdk

Quick start

import asyncio

from mycode import Agent, bash_tool, read_tool


async def main() -> None:
    agent = Agent(
        model="claude-sonnet-4-6",
        api_key="YOUR_API_KEY",
        tools=[read_tool, bash_tool],
    )

    async for event in agent.achat("Read pyproject.toml and tell me the project name."):
        if event.type == "text":
            print(event.data["delta"], end="", flush=True)


asyncio.run(main())

Agent(...) infers the provider from the model id. No tools are registered unless you pass tools=[...].

For a simple synchronous call, use run():

result = agent.run("Read pyproject.toml and tell me the project name.")
print(result.text)

Multi-turn conversations

Call achat() or run() again on the same Agent — history accumulates automatically:

agent = Agent(model="claude-sonnet-4-6", api_key="...")

agent.run("What is 2 + 2?")
agent.run("Now multiply that by 10.")    # remembers the earlier answer

Saving sessions

Pass session_dir to persist the conversation to disk. Each session lives in a subdirectory named by session_id:

from pathlib import Path

agent = Agent(
    model="claude-sonnet-4-6",
    api_key="...",
    session_dir=Path("./chats"),
    session_id="my-chat",
)

Construct another Agent with the same (session_dir, session_id) later to resume the conversation — the history is loaded automatically.

Built-in tools

from mycode import read_tool, write_tool, edit_tool, bash_tool

Four tools for reading, writing, editing files, and running shell commands. Opt in by passing them to tools=[...].

Custom tools

Decorate any function with @tool. Parameter type hints become the JSON schema sent to the provider; the docstring becomes the description:

from mycode import Agent, tool


@tool
def greet(name: str) -> str:
    """Return a friendly greeting."""

    return f"hello, {name}"


agent = Agent(
    model="claude-sonnet-4-6",
    api_key="...",
    tools=[greet],
)

To call a built-in tool from inside your own, type the first parameter as ToolContext:

from mycode import ToolContext, tool


@tool
def summarize_file(ctx: ToolContext, path: str) -> str:
    """Return the first line of a text file."""

    result = ctx.read(path)
    return result.output.splitlines()[0] if result.output else ""

Tool hooks

Inspect or replace tool calls before they run. Return None from before_tool to let the tool execute, or a ToolExecutionResult to skip it:

from mycode import Agent, Hooks, ToolExecutionResult, bash_tool

hooks = Hooks()


@hooks.before_tool
async def block_rm(ctx):
    cmd = str(ctx.tool_input.get("command") or "")
    if ctx.tool_name == "bash" and "rm -rf" in cmd:
        return ToolExecutionResult(output="error: blocked", is_error=True)
    return None


agent = Agent(
    model="claude-sonnet-4-6",
    api_key="...",
    tools=[bash_tool],
    hooks=hooks,
)

@hooks.after_tool runs after the tool and can replace the result (audit, redact, etc.).

See docs/sdk.md for the event stream, cancellation, sessions, and the full Agent / @tool reference.

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

mycode_sdk-0.7.1.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

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

mycode_sdk-0.7.1-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file mycode_sdk-0.7.1.tar.gz.

File metadata

  • Download URL: mycode_sdk-0.7.1.tar.gz
  • Upload date:
  • Size: 47.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mycode_sdk-0.7.1.tar.gz
Algorithm Hash digest
SHA256 459e06bfdb30e93303178f2c23a95bf8460544010e7a755c3c2c1e2bde547aea
MD5 4b407848ac79b3be10b7e40c80fe5957
BLAKE2b-256 2a2a58ab6e48ba52a52e16b21e7edc3a774c2d9ab9542de62aafb44e9f1d6fca

See more details on using hashes here.

File details

Details for the file mycode_sdk-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: mycode_sdk-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mycode_sdk-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e24d0e00daf19ed4a001df140771ab4756650c59882ce5a1e2cba79f024cce17
MD5 8567cdceb165e15f58439aac3ce7aec3
BLAKE2b-256 4303a31cd8398c843aa909a1fa055f28a025cef1b2c30c13db2c06f440bc0046

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