An application can replace the tools it supplies to a live session without recreating it. For example, a web page can offer different tools as the user navigates, or a client that takes over a session can bring its own tools. Each SDK replaces the client's complete tool set and the handlers that serve it in one call.
Experimental. Tool replacement wraps the experimental
session.tools.setRPC and requires a runtime that supports it.
Pass the complete set of tools this client should supply. It replaces the tools the client supplied when the session was created or resumed, or in an earlier replacement. Built-in, MCP, and plugin tools, and tools that other clients connected to the same session supply, are unaffected. Pass an empty list to remove all of this client's tools.
Tools are defined exactly as they are for creating a session, and a tool without a handler behaves the same way it does there.
import type { CopilotSession } from "copilot-sdk-supercharged";
declare function findIssues(query: string): Promise<string>;
declare const session: CopilotSession;
import { defineTool } from "copilot-sdk-supercharged";
import { z } from "zod";
const searchIssues = defineTool("search_issues", {
description: "Search the issues shown on the current page",
parameters: z.object({ query: z.string().describe("Search text") }),
handler: async ({ query }) => findIssues(query),
});
// Supply only the tools the current page provides.
await session.setTools([searchIssues]);
// Remove all of this client's tools.
await session.setTools([]);
import { defineTool } from "copilot-sdk-supercharged";
import { z } from "zod";
const searchIssues = defineTool("search_issues", {
description: "Search the issues shown on the current page",
parameters: z.object({ query: z.string().describe("Search text") }),
handler: async ({ query }) => findIssues(query),
});
// Supply only the tools the current page provides.
await session.setTools([searchIssues]);
// Remove all of this client's tools.
await session.setTools([]);
from copilot import CopilotClient
from copilot.session import PermissionHandler
from pydantic import BaseModel, Field
from copilot.tools import define_tool
async def find_issues(query: str) -> str:
return query
class SearchIssuesParams(BaseModel):
query: str = Field(description="Search text")
@define_tool(description="Search the issues shown on the current page")
async def search_issues(params: SearchIssuesParams) -> str:
return await find_issues(params.query)
async def main() -> None:
client = CopilotClient(mode="coding-agent")
session = await client.create_session(
permissions={"on_request": PermissionHandler.approve_all}
)
# Supply only the tools the current page provides.
await session.set_tools([search_issues])
# Remove all of this client's tools.
await session.set_tools([])
from pydantic import BaseModel, Field
from copilot.tools import define_tool
class SearchIssuesParams(BaseModel):
query: str = Field(description="Search text")
@define_tool(description="Search the issues shown on the current page")
async def search_issues(params: SearchIssuesParams) -> str:
return await find_issues(params.query)
# Supply only the tools the current page provides.
await session.set_tools([search_issues])
# Remove all of this client's tools.
await session.set_tools([])
package main
import (
"context"
"log"
copilot "github.com/microsoft/copilot-sdk/go/v2"
)
type SearchIssuesParams struct {
Query string `json:"query" jsonschema:"Search text"`
}
func findIssues(query string) (string, error) {
return query, nil
}
func main() {
ctx := context.Background()
client := copilot.NewClient(&copilot.ClientOptions{Mode: copilot.ModeCodingAgent})
session, err := client.CreateSession(ctx, &copilot.CreateSessionOptions{
Permissions: copilot.SessionPermissionOptions{
OnRequest: copilot.PermissionHandler.ApproveAll,
},
})
if err != nil {
log.Fatal(err)
}
searchIssues := copilot.DefineTool(
"search_issues",
"Search the issues shown on the current page",
func(params SearchIssuesParams, inv copilot.ToolInvocation) (string, error) {
return findIssues(params.Query)
},
)
// Supply only the tools the current page provides.
if err := session.SetTools(ctx, []copilot.Tool{searchIssues}); err != nil {
log.Fatal(err)
}
// Remove all of this client's tools.
if err := session.SetTools(ctx, nil); err != nil {
log.Fatal(err)
}
}
type SearchIssuesParams struct {
Query string `json:"query" jsonschema:"Search text"`
}
searchIssues := copilot.DefineTool(
"search_issues",
"Search the issues shown on the current page",
func(params SearchIssuesParams, inv copilot.ToolInvocation) (string, error) {
return findIssues(params.Query)
},
)
// Supply only the tools the current page provides.
if err := session.SetTools(ctx, []copilot.Tool{searchIssues}); err != nil {
log.Fatal(err)
}
// Remove all of this client's tools.
if err := session.SetTools(ctx, nil); err != nil {
log.Fatal(err)
}
#pragma warning disable MCPSDK001
using System.ComponentModel;
using Microsoft.Copilot;
using Microsoft.Extensions.AI;
await using var client = new CopilotClient(new CopilotClientOptions { Mode = CopilotClientMode.CodingAgent });
await using var session = await client.CreateSessionAsync(new CreateSessionOptions
{
Permissions = new SessionPermissionOptions
{
OnRequest = PermissionHandler.ApproveAll
}
});
var searchIssues = CopilotTool.DefineTool(([Description("Search text")] string query) => FindIssues(query), factoryOptions: new AIFunctionFactoryOptions
{
Name = "search_issues",
Description = "Search the issues shown on the current page",
});
// Supply only the tools the current page provides.
await session.SetToolsAsync([searchIssues]);
// Remove all of this client's tools.
await session.SetToolsAsync([]);
static string FindIssues(string query) => query;
#pragma warning restore MCPSDK001
var searchIssues = CopilotTool.DefineTool(([Description("Search text")] string query) => FindIssues(query), factoryOptions: new AIFunctionFactoryOptions
{
Name = "search_issues",
Description = "Search the issues shown on the current page",
});
// Supply only the tools the current page provides.
await session.SetToolsAsync([searchIssues]);
// Remove all of this client's tools.
await session.SetToolsAsync([]);
SetToolsAsync raises the MCPSDK001 experimental diagnostic, which you suppress with #pragma warning disable MCPSDK001 or a project-level <NoWarn>MCPSDK001</NoWarn>.
import com.microsoft.copilot.rpc.CopilotClientOptions;
import com.microsoft.copilot.rpc.CopilotClientMode;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import com.microsoft.copilot.AllowCopilotExperimental;
import com.microsoft.copilot.CopilotClient;
import com.microsoft.copilot.CopilotSession;
import com.microsoft.copilot.rpc.PermissionHandler;
import com.microsoft.copilot.rpc.CreateSessionOptions;
import com.microsoft.copilot.rpc.ToolDefinition;
import com.microsoft.copilot.rpc.SessionPermissionOptions;
@AllowCopilotExperimental
public class ChangingToolsExample {
public static void main(String[] args) throws Exception {
try (var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.CODING_AGENT))) {
CopilotSession session = client
.createSession(new CreateSessionOptions()
.setPermissions(new SessionPermissionOptions()
.setOnRequest(PermissionHandler.APPROVE_ALL)))
.get();
var searchIssues = ToolDefinition
.create("search_issues", "Search the issues shown on the current page", Map
.of("type", "object", "properties", Map
.of("query", Map
.of("type", "string", "description", "Search text")),
"required", List
.of("query")),
invocation -> CompletableFuture
.completedFuture(findIssues((String) invocation
.getArguments()
.get("query"))));
// Supply only the tools the current page provides.
session
.setTools(List
.of(searchIssues))
.get();
// Remove all of this client's tools.
session
.setTools(List
.of())
.get();
}
}
private static String findIssues(String query) {
return query;
}
}
var searchIssues = ToolDefinition
.create("search_issues", "Search the issues shown on the current page", Map
.of("type", "object", "properties", Map
.of("query", Map
.of("type", "string", "description", "Search text")),
"required", List
.of("query")),
invocation -> CompletableFuture
.completedFuture(findIssues((String) invocation
.getArguments()
.get("query"))));
// Supply only the tools the current page provides.
session
.setTools(List
.of(searchIssues))
.get();
// Remove all of this client's tools.
session
.setTools(List
.of())
.get();
setTools is @CopilotExperimental, so the consuming class or method must opt in with @AllowCopilotExperimental. See Using experimental APIs.
use serde::Deserialize;
async fn find_issues(query: &str) -> String {
query.to_owned()
}
#[derive(Deserialize, JsonSchema)]
struct SearchIssuesParams {
/// Search text
query: String,
}
let search_issues = define_tool(
"search_issues",
"Search the issues shown on the current page",
|_inv, params: SearchIssuesParams| async move {
Ok(ToolResult::Text(find_issues(¶ms.query).await))
},
);
// Supply only the tools the current page provides.
session.set_tools(vec![search_issues]).await?;
// Remove all of this client's tools.
session.set_tools(Vec::new()).await?;
#[derive(Deserialize, JsonSchema)]
struct SearchIssuesParams {
/// Search text
query: String,
}
let search_issues = define_tool(
"search_issues",
"Search the issues shown on the current page",
|_inv, params: SearchIssuesParams| async move {
Ok(ToolResult::Text(find_issues(¶ms.query).await))
},
);
// Supply only the tools the current page provides.
session.set_tools(vec![search_issues]).await?;
// Remove all of this client's tools.
session.set_tools(Vec::new()).await?;
A model request that is already in flight was made with the previous tools, so the agent can still call a tool you just removed. If no connected client supplies a tool with that name any more, the call fails with an error that tells the agent the tool is no longer available, and the turn continues.
If you don't want the agent to see that failure, replace tools while the session is idle, for example after the session.idle event, or abort the turn first. Replacing a tool's handler under the same name is safe during a turn: calls dispatched after the runtime accepts the replacement use the new handler.
| SDK | Method |
|---|---|
| TypeScript | session.setTools(tools) |
| Python | await session.set_tools(tools) |
| Go | session.SetTools(ctx, tools) |
| .NET | await session.SetToolsAsync(tools, cancellationToken) |
| Java | session.setTools(tools), which returns a CompletableFuture<Void> |
| Rust | session.set_tools(tools).await |
Can you improve this documentation? These fine people already did:
copilot-cli-release-app[bot], Jeremiah Isaacson & copilot-sdk-mirror-writer[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 |