Skip to main content
When creating a session, you can mount one or more GitHub repositories into the session sandbox as github_repository resources. The agent can read code, modify files, and run tests, which is useful for code review, bug fixes, and code migration tasks. Mounting a repository only prepares the code. To create pull requests, read issues, or perform other GitHub operations, configure MCP and Vaults separately.

Prerequisites

Before you start, make sure you have:
  • A Kimi API key with access to Hosted Agents; when using the kha CLI, you can run kha login --base-url https://api.moonshot.ai first instead.
  • An agent ID.
  • A cloud execution environment ID whose build is ready.
  • A GitHub repository. To mount a private repository, you also need a GitHub token with read access to the repository contents.
The examples below show both cURL and kha CLI commands. Before using the CLI, install and authenticate, then set the resource IDs shared by the examples:

Mount a public repository

The example below mounts a public repository when creating a session. If you omit checkout, the remote default branch is used; if you omit mount_path, the repository is cloned to /workspace/<repo-name>.
On success, the endpoint returns HTTP 201 and the session object. Save the session id from the response as SESSION_ID. In this example, the repository directory is /workspace/<repo>, where you can have the agent read code, modify files, and run tests.
A successful session creation response only means the request passed validation and was persisted; it does not mean the repository has finished cloning. Repositories are prepared asynchronously in the background, and failures are reported through session.error events. See Handle repository preparation errors.
A github_repository resource accepts the following fields: url does not accept SSH URLs, .git suffixes, trailing slashes, userinfo, query strings, fragments, extra path segments, or ports. mount_path is the final directory, not its parent directory. For example, if you mount a repository at /workspace/service, a README.md file at the repository root is located at /workspace/service/README.md. Mount paths of different repositories cannot be identical or nested inside one another.

Mount a private repository

Private repositories require a GitHub token with content read permission passed via authorization_token. The cURL example below reads the token from the GITHUB_TOKEN environment variable, and the CLI example passes it via standard input so the token does not appear in command-line arguments.
Prefer --repo-token-stdin with the CLI. Using --repo-token can leave the token in your shell history or process arguments. Do not put tokens in prompts, client logs, or any other content that enters the model context.
The token is used only for authenticating repository access. It never appears in resource query responses and is not automatically used as a credential for GitHub MCP. Grant the minimum permissions required for the task: mounting a private repository requires content read permission, while operations such as creating a pull request require additional permissions determined by your GitHub workflow.

Select a branch or commit

Use a branch

Specify the branch name in checkout. With the CLI’s --repo flag, you can append :<branch> directly to the repository URL.

Use a commit

To pin a commit, sha must be the full 40-character SHA. In the CLI, use the general --resource flag to pass the complete resource object; this flag accepts JSON and relaxed YAML and can be repeated.

Mount multiple repositories

Add multiple repository objects to resources, or repeat --repo in the CLI:
The example above clones the repositories into /workspace/frontend and /workspace/backend, respectively. In the current implementation, each session can bind at most 4 GitHub repositories by default, and a session cannot bind the same normalized repository URL more than once. Repositories with the same name under different owners cause a default path conflict, so you must set distinct mount_path values explicitly. When using the CLI, the token read by --repo-token-stdin applies to all --repo flags. If each repository requires a different token, submit the full request body with --file and set authorization_token on each resource separately. Invalid URLs, checkout values, or mount_path values, mount path conflicts, duplicate repository URLs, and using a self-hosted environment (which does not support automatic repository mounting) are rejected synchronously when creating a session or creating or updating a trigger configuration. Field-level errors for duplicate URLs point to resources[i].url, while default path conflicts and other mount path conflicts point to resources[i].mount_path, where i is the resource’s index in the resources array.

Query repository bindings

List a session’s resources, or query a single repository by its resource binding id:
RESOURCE_ID is the binding id returned when creating the session or listing its resources. Query responses never return authorization_token. API responses include created_at and updated_at, but do not expose repository preparation status or the commit that the platform first resolved and uses for rebuilds. When the request specifies a branch, checkout in the response still reflects the requested branch rather than the commit it resolved to. Repository preparation failures are reported through session.error; see Handle repository preparation errors. Repository bindings cannot be added, modified, or deleted after the session is created. To change the URL, branch, commit, mount path, or token, create a new session.

Handle repository preparation errors

The platform prepares the sandbox and repositories in the background. Inspect session events to check whether a repository-related session.error occurred:
When the session sandbox is subsequently used, the platform retries any unfinished repository preparation; querying the session or its resources does not trigger this process. Handle errors based on data.error_type: For event subscription, reading history, and reconnecting after a disconnect, see Event Stream.

Use skills from a repository

After a repository is prepared for the first time, the platform discovers SKILL.md files in the repository, parses their names and descriptions, and records each skill’s path along with its repository and the first successfully checked-out commit. During discovery, at most 1 MiB of each SKILL.md is read to parse its frontmatter; files that remain parseable after truncation are included, while incomplete or unparseable files are skipped. Reading the full file later is still subject to the limits of the file-reading tool. A normal resume does not rescan the repository. Adding or updating skills in the remote repository does not automatically refresh the set of skills already discovered in the current session; create a new session to refresh it. When a repo skill has the same name as a skill explicitly attached to the agent (an attached skill), both are retained; skills with the same name from different repositories can also coexist. The skill list injected into the model preserves each skill’s name, description, and SKILL.md path. The model selects a skill based on the task and description, then reads the corresponding path. Skills are neither overwritten because of duplicate names nor deduplicated by repository preparation completion order. For the catalog and naming rules of attached skills, see Skills.

Workspace recovery and code versions

Repository recovery follows these rules:
  • Normal recovery: When a valid persistent checkpoint exists, the platform does not re-clone the repository and restores the full workspace, including uncommitted changes and unpushed local commits. Restoring auxiliary metadata does not change HEAD or working files, and does not automatically sync the upstream branch.
  • Fresh sandbox: When no usable checkpoint exists and the mount directory is completely absent, the repository is rebuilt from the first successfully checked-out commit. That commit is the rebuild baseline: it does not represent the workspace’s current HEAD and does not change when the remote branch advances, the agent runs git fetch or git pull, or local commits are created.
  • Preserve existing state: If the mount directory still exists, the platform does not automatically delete and rebuild it even when auxiliary metadata is missing or the Git working tree is unusable; it preserves the directory and reports an error.
  • Recovery boundary: Workspace changes that were not saved to a persistent checkpoint cannot be recovered. Event history is not a filesystem backup.
To update code from upstream, have the agent run git fetch or git pull explicitly rather than relying on the recovery process to move the branch HEAD.

Use GitHub workflows

Repository resources do not automatically configure GitHub MCP or create pull requests. To use those capabilities, first declare GitHub MCP on the agent and configure authentication through a vault, then have the agent create branches, commit changes, or open pull requests based on the mounted repository. For configuration steps, see Configure MCP Servers and Vaults. Repository resources and MCP are authenticated separately: the authorization_token used to mount a repository is not automatically provided to MCP.

Limitations

  • Self-hosted environments do not support automatic repository mounting.
  • Git submodules are not initialized automatically.
  • Git LFS objects are not pulled automatically.
  • GitHub MCP is not configured and pull requests are not created automatically.
  • git pull is not run automatically when the remote branch advances. Have the agent run git fetch or git pull explicitly when an update is needed.
  • When reusing the github_repository structure in triggers, repositories are also prepared asynchronously in the background, so you must continue to handle error events for the corresponding sessions.

Next steps

Connect GitHub MCP

Configure external tools on your agent for GitHub workflows.

Handle session events

Subscribe to execution progress and handle asynchronous repository preparation errors.

Schedule repository tasks

Run code inspection and maintenance tasks on a schedule with triggers.