Commands the agent runs have no terminal, so sudo, ssh, and git can't ask for a password, and they fail. With a shell authentication handler, your app can show those prompts to the user instead. The answer goes only to the program that asked: the model never sees it, and it isn't recorded in session events.
The handler is called for these prompts:
sudo -A passwords on macOS and Linuxssh passwords, key passphrases, and host-key confirmationsgit user names and passwords for HTTP and HTTPS remotesSet the handler in the session configuration. In these examples, the text and yes-or-no functions stand for your app's own dialogs, and each one closes when the request is cancelled.
import { CopilotClient } from "copilot-sdk-supercharged";
// Your app's own dialogs. Each closes when `signal` aborts.
declare function askForText(options: {
message: string;
masked: boolean;
signal: AbortSignal;
}): Promise<string | undefined>;
declare function askYesNo(message: string, signal: AbortSignal): Promise<boolean>;
const client = new CopilotClient({ mode: "coding-agent" });
await client.start();
const session = await client.createSession({
permissions: {
onRequest: async () => ({ kind: "approve-once" }),
},
interaction: {
onShellAuthenticationRequest: async (request, { signal }) => {
const message = `${request.program} asks: ${request.prompt}`;
if (request.inputKind === "confirm") {
return (await askYesNo(message, signal))
? { action: "accept", value: "yes" }
: { action: "cancel" };
}
const value = await askForText({
message,
masked: request.inputKind === "secret",
signal,
});
return value === undefined ? { action: "cancel" } : { action: "accept", value };
},
},
});
import asyncio
from copilot import (
CopilotClient,
ShellAuthenticationInvocation,
ShellAuthenticationRequest,
ShellAuthenticationResponse,
)
from copilot.session import PermissionHandler
# Your app's own dialogs. Each closes when `cancelled` is set.
async def ask_for_text(
message: str, *, masked: bool, cancelled: asyncio.Event
) -> str | None:
raise NotImplementedError
async def ask_yes_no(message: str, cancelled: asyncio.Event) -> bool:
raise NotImplementedError
async def on_shell_authentication_request(
request: ShellAuthenticationRequest, invocation: ShellAuthenticationInvocation
) -> ShellAuthenticationResponse:
message = f"{request['program']} asks: {request['prompt']}"
cancelled = invocation["cancelled"]
if request["inputKind"] == "confirm":
if await ask_yes_no(message, cancelled):
return {"action": "accept", "value": "yes"}
return {"action": "cancel"}
value = await ask_for_text(
message,
masked=request["inputKind"] == "secret",
cancelled=cancelled,
)
if value is None:
return {"action": "cancel"}
return {"action": "accept", "value": value}
async def main():
client = CopilotClient(mode="coding-agent")
await client.start()
session = await client.create_session(
permissions={"on_request": PermissionHandler.approve_all},
interaction={
"on_shell_authentication_request": on_shell_authentication_request
},
)
asyncio.run(main())
package main
import (
"context"
"fmt"
copilot "github.com/microsoft/copilot-sdk/go/v2"
)
// Your app's own dialogs. Each closes when ctx is done.
func askForText(ctx context.Context, message string, masked bool) (string, bool) {
panic("not implemented")
}
func askYesNo(ctx context.Context, message string) bool {
panic("not implemented")
}
func onShellAuthenticationRequest(
request copilot.ShellAuthenticationRequest,
invocation copilot.ShellAuthenticationInvocation,
) (copilot.ShellAuthenticationResponse, error) {
cancel := copilot.ShellAuthenticationResponse{Action: copilot.ShellAuthenticationActionCancel}
message := fmt.Sprintf("%s asks: %s", request.Program, request.Prompt)
if request.InputKind == copilot.ShellAuthenticationInputKindConfirm {
if !askYesNo(invocation.Context, message) {
return cancel, nil
}
return copilot.ShellAuthenticationResponse{Action: copilot.ShellAuthenticationActionAccept, Value: "yes"}, nil
}
masked := request.InputKind == copilot.ShellAuthenticationInputKindSecret
value, ok := askForText(invocation.Context, message, masked)
if !ok {
return cancel, nil
}
return copilot.ShellAuthenticationResponse{Action: copilot.ShellAuthenticationActionAccept, Value: value}, nil
}
func main() {
ctx := context.Background()
client := copilot.NewClient(&copilot.ClientOptions{Mode: copilot.ModeCodingAgent})
if err := client.Start(ctx); err != nil {
panic(err)
}
defer client.Stop()
session, err := client.CreateSession(ctx, &copilot.CreateSessionOptions{
Permissions: copilot.SessionPermissionOptions{
OnRequest: copilot.PermissionHandler.ApproveAll,
},
Interaction: copilot.SessionInteractionOptions{
OnShellAuthenticationRequest: onShellAuthenticationRequest,
},
})
if err != nil {
panic(err)
}
_ = session
}
using Microsoft.Copilot;
await using var client = new CopilotClient(new CopilotClientOptions { Mode = CopilotClientMode.CodingAgent });
await client.StartAsync();
await using var session = await client.CreateSessionAsync(new CreateSessionOptions
{
Permissions = new SessionPermissionOptions
{
OnRequest = PermissionHandler.ApproveAll
},
Interaction = new SessionInteractionOptions
{
OnShellAuthenticationRequest = async (request, cancellationToken) =>
{
var message = $"{request.Program} asks: {request.Prompt}";
if (request.InputKind == ShellAuthenticationInputKind.Confirm)
{
return await AskYesNoAsync(message, cancellationToken) ? ShellAuthenticationResponse.Accept("yes") : ShellAuthenticationResponse.Cancel();
}
var value = await AskForTextAsync(message, masked: request.InputKind == ShellAuthenticationInputKind.Secret, cancellationToken);
return value is null ? ShellAuthenticationResponse.Cancel() : ShellAuthenticationResponse.Accept(value);
}
}
});
// Your app's own dialogs. Each closes when the token is cancelled.
static Task<string?> AskForTextAsync(string message, bool masked, CancellationToken cancellationToken) => throw new NotImplementedException();
static Task<bool> AskYesNoAsync(string message, CancellationToken cancellationToken) => throw new NotImplementedException();
import com.microsoft.copilot.CopilotClient;
import com.microsoft.copilot.rpc.CopilotClientOptions;
import com.microsoft.copilot.rpc.CopilotClientMode;
import com.microsoft.copilot.rpc.PermissionHandler;
import com.microsoft.copilot.rpc.ShellAuthenticationResponse;
import com.microsoft.copilot.rpc.CreateSessionOptions;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import com.microsoft.copilot.rpc.SessionInteractionOptions;
import com.microsoft.copilot.rpc.SessionPermissionOptions;
public class ShellPasswordPromptsExample {
public static void main(String[] args) throws Exception {
try (var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.CODING_AGENT))) {
client
.start()
.get();
var session = client
.createSession(new CreateSessionOptions()
.setPermissions(new SessionPermissionOptions()
.setOnRequest(PermissionHandler.APPROVE_ALL))
.setInteraction(new SessionInteractionOptions()
.setOnShellAuthenticationRequest((request, invocation) -> {
var message = request
.getProgram() + " asks: "
+ request
.getPrompt();
if ("confirm"
.equals(request
.getInputKind())) {
return askYesNo(message, invocation
.getCancellation())
.thenApply(yes -> yes ? ShellAuthenticationResponse
.accept("yes")
: ShellAuthenticationResponse
.cancel());
}
var masked = "secret"
.equals(request
.getInputKind());
return askForText(message, masked, invocation
.getCancellation())
.thenApply(value -> value
.map(ShellAuthenticationResponse::accept)
.orElseGet(ShellAuthenticationResponse::cancel));
})))
.get();
}
}
// Your app's own dialogs. Each closes when `cancellation` completes.
static CompletableFuture<Optional<String>> askForText(String message, boolean masked, CompletionStage<Void> cancellation) {
throw new UnsupportedOperationException();
}
static CompletableFuture<Boolean> askYesNo(String message, CompletionStage<Void> cancellation) {
throw new UnsupportedOperationException();
}
}
use std::sync::Arc;
use async_trait::async_trait;
use copilot_sdk::handler::{
ShellAuthenticationHandler, ShellAuthenticationInputKind, ShellAuthenticationRequest,
ShellAuthenticationResponse,
};
use copilot_sdk::types::{CreateSessionOptions, SessionId};
use tokio_util::sync::CancellationToken;
// Your app's own dialogs. Each closes when `cancellation` fires.
async fn ask_for_text(
message: &str,
masked: bool,
cancellation: &CancellationToken,
) -> Option<String> {
todo!()
}
async fn ask_yes_no(message: &str, cancellation: &CancellationToken) -> bool {
todo!()
}
struct PasswordPrompts;
#[async_trait]
impl ShellAuthenticationHandler for PasswordPrompts {
async fn handle(
&self,
_session_id: SessionId,
request: ShellAuthenticationRequest,
cancellation: CancellationToken,
) -> ShellAuthenticationResponse {
let message = format!("{} asks: {}", request.program, request.prompt);
let answer = if request.input_kind == ShellAuthenticationInputKind::Confirm {
ask_yes_no(&message, &cancellation)
.await
.then(|| "yes".to_owned())
} else {
let masked = request.input_kind == ShellAuthenticationInputKind::Secret;
ask_for_text(&message, masked, &cancellation).await
};
match answer {
Some(value) => ShellAuthenticationResponse::Accept(value),
None => ShellAuthenticationResponse::Cancel,
}
}
}
let session = client
.create_session(
CreateSessionOptions::default()
.with_permissions(|__sdk_group| __sdk_group.approve_all_permissions())
.with_interaction(|__sdk_group| {
__sdk_group.with_on_shell_authentication_request(Arc::new(PasswordPrompts))
}),
)
.await?;
The request has these fields. Each SDK follows its own naming conventions, for example input_kind in Rust:
| Field | Description |
|---|---|
program | sudo, ssh, or git |
prompt | The prompt text the program supplied |
inputKind | secret for a password or passphrase, which you mask. text for a visible answer, such as a user name. confirm for a yes or no question: answer "yes", or cancel to decline |
command | The command that triggered the prompt |
cwd | The command's working directory |
The handler's cancellation signal fires when the answer is no longer needed, for example because the command ended, the prompt timed out after 10 minutes, or the session ended or switched to autopilot. Close the input when it does. The signal is signal in TypeScript, cancelled in Python, the invocation's Context in Go, the handler's CancellationToken parameter in .NET, getCancellation() in Java, and the cancellation token in Rust. If the handler fails or returns a malformed answer, the prompt is cancelled, so the program that asked fails instead of receiving a wrong value. The SDK logs a handler failure with only the error's type, never its message, which could contain the answer. In Rust, the process's panic hook still reports a handler panic, by default by printing its message to stderr, so don't put the answer in a panic message.
A command asks the user only when someone can answer. Sessions without a shell authentication handler, and sessions in autopilot, never wait for input: a command that needs a password fails as it would without this feature. On macOS and Linux, the agent in a session that can show these prompts is told to run commands that need sudo with sudo -A, so sudo asks through your handler instead of failing. The handler is called when the SDK is connected to the Copilot runtime, whether the SDK hosts it in process, as every SDK does by default, or runs copilot-runtime as a separate process. In the interactive Copilot CLI, this is experimental: after /experimental on, the session you're viewing shows these prompts in a dialog that masks passwords. Non-interactive copilot -p runs never show them.
Every SDK can set a shell authentication handler:
| SDK | Handler option |
|---|---|
| Node.js / TypeScript | onShellAuthenticationRequest |
| Python | on_shell_authentication_request |
| Go | OnShellAuthenticationRequest |
| .NET | OnShellAuthenticationRequest |
| Java | setOnShellAuthenticationRequest |
| Rust | with_on_shell_authentication_request |
Can you improve this documentation? These fine people already did:
copilot-sdk-mirror-writer[bot], Jeremiah Isaacson & copilot-cli-release-app[bot]Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |