Copilot Supercharged SDK for Elixir

Elixir SDK for the GitHub Copilot CLI. Communicates with the Copilot CLI server via JSON-RPC 2.0 over stdio.

Requirements

Installation

Add to your mix.exs dependencies:

def deps do
  [
    {:copilot_sdk_supercharged, path: "../elixir"}  # or from Hex when published
  ]
end

Then fetch dependencies:

mix deps.get

Quick Start

alias Copilot.Client
alias Copilot.Session
alias Copilot.DefineTool
alias Copilot.Types.{CopilotClientOptions, SessionConfig, MessageOptions}

# Start the client (spawns the CLI process automatically)
{:ok, client} = Client.start_link(%CopilotClientOptions{log_level: "info"})

# Create a session
{:ok, session} = Client.create_session(client, %SessionConfig{})

# Subscribe to events
Session.on(session, fn event ->
  if event["type"] == "assistant.message" do
    IO.puts("Assistant: " <> event["data"]["content"])
  end
end)

# Send a message and wait for the response
{:ok, response} = Session.send_and_wait(session, %MessageOptions{prompt: "What is 2+2?"})
IO.puts("Response: #{response["data"]["content"]}")

# Clean up
Session.destroy(session)
Client.stop(client)

What's New in v3.1.0

Parity with the Microsoft Copilot SDK 2.0.3-preview.0. The six new Copilot.Client functions below call server-scoped RPCs that require Copilot CLI 1.0.116 or newer; older CLIs answer with a JSON-RPC method-not-found error, returned as {:error, error} like any other RPC error.

MCP secret store and plugin extension approvals

alias Copilot.Client

# MCP secret store. The value is a credential: it is sent to the runtime and never logged.
{:ok, saved} =
  Client.mcp_secrets_save(client, "my-server/api-key", System.fetch_env!("MY_API_KEY"),
    config_dir: "/path/to/config"
  )

IO.puts(if saved.used_keychain, do: "Stored in the OS keychain", else: "Stored in the file backend")

:ok = Client.mcp_secrets_delete(client, "my-server/api-key")
:ok = Client.mcp_secrets_delete_server(client, "my-server")

# Plugin extension approvals (user scoped). Grant persists a pending extension
# permission request before you answer it.
{:ok, grant} = Client.extension_approvals_grant(client, session_id, request_id)
if grant.granted, do: IO.puts("Approved #{grant.approval.extension_name}")

{:ok, %{approvals: approvals}} = Client.extension_approvals_list(client)

for approval <- approvals do
  {:ok, %{revoked: true}} =
    Client.extension_approvals_revoke(client, approval.extension_name, approval.installation_key)
end
Function Wire method Returns
Client.mcp_secrets_save/4 (option config_dir:) mcp.secrets.save {:ok, %McpSecretsSaveResult{used_keychain: bool}} (false means the file backend stored it)
Client.mcp_secrets_delete/3 (option config_dir:) mcp.secrets.delete :ok
Client.mcp_secrets_delete_server/3 (option config_dir:) mcp.secrets.deleteServer :ok
Client.extension_approvals_grant/3 permissions.extensionApprovals.grant {:ok, %PermissionsExtensionApprovalsGrantResult{granted, approval}}
Client.extension_approvals_list/2 (option session_id:) permissions.extensionApprovals.list {:ok, %PermissionsExtensionApprovalsListResult{approvals}}
Client.extension_approvals_revoke/4 (option session_id:) permissions.extensionApprovals.revoke {:ok, %PermissionsExtensionApprovalsRevokeResult{revoked: bool}}

Each %PermissionsExtensionApproval{} has extension_name (the stable identity, for example "plugin:my-plugin:my-extension"), plugin_name, capabilities, granted_at (RFC 3339), installation_key (opaque: pass it back unchanged to revoke), and installation_label.

Ask-user image attachments

When the runtime accepts images, the %UserInputRequest{} passed to on_user_input_request carries image_attachment_constraints (%UserInputImageAttachmentConstraints{allowed_mime_types, max_bytes_per_image, max_count}); it is nil otherwise. The request also exposes request_id and meta (the opaque _meta map) when the runtime sends them. Answer with attachments, a list of %UserInputImageAttachment{} (base64 data, mime_type, optional description and display_name; the wire type is always "image"). attachments is sent only when set and is only valid when the request advertised constraints; the runtime enforces the limits.

alias Copilot.Types.{UserInputImageAttachment, UserInputRequest, UserInputResponse}

config = %SessionConfig{
  on_user_input_request: fn %UserInputRequest{image_attachment_constraints: constraints}, _ctx ->
    if constraints && "image/png" in constraints.allowed_mime_types do
      image = %UserInputImageAttachment{
        data: Base.encode64(File.read!("screenshot.png")),
        mime_type: "image/png",
        display_name: "screenshot.png"
      }

      # Image-only answer: empty answer and was_freeform true
      %UserInputResponse{answer: "", was_freeform: true, attachments: [image]}
    else
      %UserInputResponse{answer: "No screenshot available", was_freeform: true}
    end
  end
}

What's New in v3.0.0

This release speaks SDK protocol 4, the contract of the Microsoft Copilot SDK 2.0 runtime, and requires Copilot CLI 1.0.113 or newer: older CLIs fail Client.start/1 with a protocol mismatch error. Existing options keep working. You fill %SessionConfig{} and %ResumeSessionConfig{} exactly as before, and Copilot.Client converts the flat payload into the grouped v4 session.create / session.resume payload right before sending (for example streaming becomes events.streaming, hooks becomes hooks.hasHandlers, and disable_resume becomes suppressResumeEvent). The legacy single provider becomes a named "default" connection with Copilot API (CAPI) models disabled, which preserves v1 BYOK behavior. A cli_path ending in .js (such as the CLI's npm-loader.js) now runs under node.

This release also brings the Elixir SDK up to the v2.6.0 feature set and fixes session event delivery: notifications are now dispatched with the client's current session list, so events reach every session.

New in this release:

Run a shared service in empty mode with client info and proxy callbacks:

alias Copilot.{Client, Session, DefineTool}

alias Copilot.Types.{
  CapiConfig,
  ClientInfo,
  CopilotClientOptions,
  GitHubToken,
  MessageOptions,
  MessageSource,
  NamedProviderConfig,
  NetworkConfig,
  PermissionModeRefusal,
  PolicyHelperFailure,
  ProviderModelConfig,
  ResumeSessionConfig,
  SandboxConfig,
  SessionConfig,
  SessionEventType,
  ToolShellOutput
}

{:ok, client} =
  Client.start_link(%CopilotClientOptions{
    mode: "empty",
    client_info: %ClientInfo{editor_name: "acme-portal", editor_version: "2.4.0"},
    network: %NetworkConfig{
      resolve_proxy: fn %{url: url} ->
        if String.contains?(url, ".internal.example"),
          do: "DIRECT",
          else: "PROXY proxy.example:8080"
      end,
      authenticate_basic: fn
        %{previous_rejected: true} -> nil
        _request -> %{username: "svc-proxy", password: System.get_env("PROXY_PASSWORD")}
      end
    }
  })

Use named providers, an idempotent tool, a GitHub token provider, shell authentication, and an application memory store:

lookup_order =
  DefineTool.define("lookup_order",
    description: "Look up an order by ID",
    parameters: %{"type" => "object", "properties" => %{"id" => %{"type" => "string"}}},
    idempotent: true,
    handler: fn %{"id" => id}, _invocation -> "Order #{id} shipped" end
  )

{:ok, session} =
  Client.create_session(client, %SessionConfig{
    model: "gateway/chat",
    available_tools: ["view", "lookup_order"],
    tools: [lookup_order],
    providers: [
      %NamedProviderConfig{
        name: "gateway",
        type: "openai",
        base_url: "https://models.example.com/v1",
        api_key_command: "print-gateway-key"
      }
    ],
    models: [
      %ProviderModelConfig{
        provider: "gateway",
        id: "chat",
        wire_model: "deployment-1",
        max_output_tokens: 8192
      }
    ],
    capi: %CapiConfig{enabled: false},
    sandbox_config: %SandboxConfig{enabled: true, allow_bypass: true},
    github_token_provider: fn %{host: _host, reason: _reason} ->
      %GitHubToken{access_token: System.fetch_env!("GH_TOKEN"), expires_in: 8 * 3600}
    end,
    on_shell_authentication_request: fn
      %{input_kind: "confirm"} -> :cancel
      %{program: "sudo"} -> {:accept, System.get_env("SUDO_PASSWORD", "")}
      _request -> :cancel
    end,
    store_memory: fn memory ->
      IO.puts("remember [#{memory.subject}]: #{memory.fact}")
      :ok
    end
  })

Stream shell output, tag a turn, switch the Auto tier, and change the permission mode:

Session.on(session, SessionEventType.tool_shell_output(), fn event ->
  IO.write(ToolShellOutput.from_map(event).text)
end)

{:ok, _message_id} =
  Session.send(session, %MessageOptions{
    prompt: "Run the test suite",
    source: MessageSource.agent("planner"),
    client_turn_id: "turn-42"
  })

{:ok, _ack} = Session.switch_auto_tier(session, "intelligence")

managed_policy = PermissionModeRefusal.managed_policy()

case Session.set_permission_mode(session, "allow-all") do
  {:ok, %{success: false, reason: ^managed_policy, mode: mode}} ->
    IO.puts("Managed policy keeps the session in #{mode} mode")

  {:ok, _applied} ->
    :ok
end

Inspect a policy helper failure, and clear the default provider on resume:

case Client.create_session(client, %SessionConfig{available_tools: ["view"]}) do
  {:ok, _session} ->
    :ok

  {:error, _} = error ->
    case PolicyHelperFailure.from_error(error) do
      %PolicyHelperFailure{category: category, message: message} ->
        IO.puts("Policy helper failed (#{category}): #{message}")

      nil ->
        error
    end
end

{:ok, _resumed} =
  Client.resume_session(client, session_id, %ResumeSessionConfig{
    available_tools: ["view"],
    default_provider: :clear,
    allow_transcript_recovery: true
  })

Known limitations

Architecture

The SDK consists of the following modules:

Module Description
Copilot.Client Main client GenServer. Spawns the CLI process, manages the JSON-RPC connection, and provides session lifecycle operations (create, resume, delete, list).
Copilot.Session Session GenServer. Sends messages, subscribes to events, handles tool calls, permissions, user input, and hooks.
Copilot.JsonRpcClient Low-level JSON-RPC 2.0 client. It opens and owns the CLI port (stdio) and frames messages with Content-Length headers (see Connection).
Copilot.Types All type definitions as Elixir structs with typespecs.
Copilot.DefineTool Helper for defining tools to expose to the CLI.
Copilot.SdkProtocolVersion Protocol version constant (must match the server).

Defining Tools

Tools allow the assistant to call custom functions you define. Use Copilot.DefineTool.define/2:

tool = Copilot.DefineTool.define("get_weather",
  description: "Get the current weather for a city.",
  parameters: %{
    "type" => "object",
    "properties" => %{
      "city" => %{"type" => "string", "description" => "City name"}
    },
    "required" => ["city"]
  },
  handler: fn %{"city" => city}, _invocation ->
    "The weather in #{city} is sunny, 72F."
  end
)

{:ok, session} = Client.create_session(client, %SessionConfig{tools: [tool]})

The handler function receives two arguments:

  1. The parsed arguments (a map matching your JSON schema)
  2. A Copilot.Types.ToolInvocation struct with session context

It can return:

Image Generation

Request image responses using response_format and image_options:

response = Copilot.Session.send_and_wait(session, %Copilot.Types.MessageOptions{
  prompt: "Generate a sunset over mountains",
  response_format: :image,
  image_options: %{size: "1024x1024", quality: "hd", style: "natural"}
})

Event Subscriptions

Subscribe to all events or specific event types:

# All events
ref = Session.on(session, fn event -> IO.inspect(event) end)

# Specific event type
ref = Session.on(session, "assistant.message", fn event ->
  IO.puts(event["data"]["content"])
end)

# Unsubscribe
Session.off(session, ref)

Common Event Types

Event Type Description
session.start Session started
session.idle Session finished processing
session.error An error occurred
user.message User message recorded
assistant.message Final assistant response
assistant.message_delta Streaming response chunk (when streaming enabled)
tool.execution_start Tool execution started
tool.execution_complete Tool execution finished

Permissions

Handle permission requests from the server:

alias Copilot.Types.{PermissionRequest, PermissionRequestResult}

config = %SessionConfig{
  on_permission_request: fn %PermissionRequest{kind: kind}, _ctx ->
    IO.puts("Permission requested: #{kind}")
    %PermissionRequestResult{kind: :approved}
  end
}

User Input (ask_user)

Handle user input requests from the agent:

alias Copilot.Types.{UserInputRequest, UserInputResponse}

config = %SessionConfig{
  on_user_input_request: fn %UserInputRequest{question: q}, _ctx ->
    answer = IO.gets("#{q} > ") |> String.trim()
    %UserInputResponse{answer: answer, was_freeform: true}
  end
}

Hooks

Intercept session lifecycle events with hooks:

alias Copilot.Types.SessionHooks

config = %SessionConfig{
  hooks: %SessionHooks{
    on_pre_tool_use: fn input, _ctx ->
      IO.puts("About to use tool: #{input["toolName"]}")
      %{"permissionDecision" => "allow"}
    end,
    on_post_tool_use: fn input, _ctx ->
      IO.puts("Tool completed: #{input["toolName"]}")
      nil
    end
  }
}

Session Management

# List all sessions
{:ok, sessions} = Client.list_sessions(client)

# Resume a previous session
{:ok, session} = Client.resume_session(client, session_id)

# Delete a session
:ok = Client.delete_session(client, session_id)

# Get the last session ID
{:ok, last_id} = Client.get_last_session_id(client)

# List available models
{:ok, models} = Client.list_models(client)

Session Idle Timeout

Configure automatic session cleanup after a period of inactivity:

{:ok, client} = CopilotClient.start_link(session_idle_timeout_seconds: 300)

SessionFs (Persistent Session Filesystem)

SessionFs provides a virtual filesystem scoped to each session, enabling persistent state across compaction boundaries and session resumes.

{:ok, client} = CopilotClient.start_link(
  session_fs: %{
    initial_cwd: "/repo",
    session_state_path: "/tmp/state",
    conventions: "posix"
  }
)

Session Metadata

Retrieve metadata about a session (model, creation time, status):

{:ok, meta} = CopilotClient.get_session_metadata(client, "session-123")

Skills and Sub-Agent Orchestration

Register skill directories and control sub-agent behavior:

{:ok, session} = CopilotClient.create_session(client,
  skill_directories: ["./skills"],
  disabled_skills: ["test-skill"],
  include_sub_agent_streaming_events: true
)

Custom Providers (BYOK)

Use your own API endpoint:

alias Copilot.Types.ProviderConfig

config = %SessionConfig{
  provider: %ProviderConfig{
    type: "openai",
    base_url: "http://localhost:11434/v1",
    api_key: "ollama"
  },
  model: "llama3"
}

Recent Features (v2.4–v2.5)

Fields added in the v2.4 and v2.5 upstream syncs. These are %SessionConfig{}, %CopilotClientOptions{}, or %MessageOptions{} fields (all under Copilot.Types).

v2.5.0

v2.4.0

Reasoning effort, content exclusion, extra directories, and rewind:

{:ok, session} = Client.create_session(client, %SessionConfig{
  reasoning_effort: "high",
  content_exclusion: true,
  additional_directories: ["../shared", "../docs"],
  rewind_enabled: true,
  disabled_mcp_servers: ["playwright"]
})

Tool search and experimental mode:

config = %SessionConfig{
  tool_search: %{"enabled" => true},
  experimental_mode: true,
  custom_agents_local_only: true
}

Session spending limits, memory, and citations:

alias Copilot.Types.{SessionConfig, SessionLimitsConfig, MemoryConfiguration}

config = %SessionConfig{
  session_limits: %SessionLimitsConfig{max_ai_credits: 5.0},
  memory: %MemoryConfiguration{enabled: true},
  enable_citations: true,
  otlp_protocol: "http/protobuf"
}

BYOK bearer-token provider and in-process transport (client options):

{:ok, client} = Client.start_link(%CopilotClientOptions{
  bearer_token_provider: fn _args -> "ghs_ephemeral_token" end,
  builtin_plugin_directories: ["./plugins"],
  in_process: false
})

Connection

Copilot.Client starts the CLI with --headless --no-auto-update --log-level <level> --stdio after any cli_args (plus --auth-token-env COPILOT_SDK_AUTH_TOKEN and --no-auto-login as github_token and use_logged_in_user require), passing one argument per item, so paths with spaces work. Copilot.JsonRpcClient opens the CLI port itself and owns it, so it receives everything the CLI writes to stdout, from the first byte. The port runs in stream mode with the Content-Length framing of vscode-jsonrpc: a frame split across reads, or several frames in one read, are both handled. The CLI's stderr is not read (it goes to the stderr of the Erlang VM), so log output never mixes with JSON-RPC frames.

test/copilot/cli_e2e_test.exs runs this connection against the real CLI and the shared replaying proxy in CI.

Protocol Version

The SDK speaks SDK protocol 4, which requires Copilot CLI 1.0.113 or newer. A server that reports another version (or none) fails Client.start/1 with {:error, "SDK protocol version mismatch: SDK supports version 4, but server reports version N. Please update your SDK or server to ensure compatibility."}.

Running the Example

cd elixir
mix deps.get
mix run examples/basic_example.exs

License

MIT - See LICENSE for details.