Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ To use `stitchTools()` with the [Vercel AI SDK](https://sdk.vercel.ai/), install
npm install @google/stitch-sdk ai
```

## Examples

See the `examples/` directory for sample code, including [Tool Filtering Custom AI Workflow Example](examples/tool-filtering/README.md).

## Working with Projects and Screens

### List existing projects
Expand Down
19 changes: 19 additions & 0 deletions packages/sdk/examples/tool-filtering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Tool Filtering Custom AI Workflow Example

This example demonstrates how to use the `stitchTools({ include: [...] })` functionality with the Vercel AI SDK to filter the available Stitch MCP tools.

Filtering tools is useful for:
- **Reducing context window usage** by only exposing the necessary tool schemas to the LLM.
- **Enforcing least privilege** so an agent can only perform specific operations (e.g., read-only access to screens without the ability to create new projects or edit design systems).

## Prerequisites

- `STITCH_API_KEY` (or `STITCH_ACCESS_TOKEN`) for the Stitch SDK.
- `GEMINI_API_KEY` (or another AI SDK provider token) for the LLM.

## How to run

```bash
# Run the example script
bun index.ts
```
65 changes: 65 additions & 0 deletions packages/sdk/examples/tool-filtering/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { stitchTools } from "@google/stitch-sdk/ai";
import { generateText } from "ai";
import { createGoogleGenerativeAI } from "@ai-sdk/google";

async function main() {
// Validate environment variables
if (!process.env.GEMINI_API_KEY) {
console.error("Error: GEMINI_API_KEY environment variable is required.");
process.exit(1);
}
if (!process.env.STITCH_API_KEY && !process.env.STITCH_ACCESS_TOKEN) {
console.error("Error: STITCH_API_KEY or STITCH_ACCESS_TOKEN environment variable is required.");
process.exit(1);
}

console.log("Configuring agent with restricted toolset...");

// Only provide the specific tools needed for this workflow to reduce
// context window usage and prevent the agent from using other tools.
const allowedTools = [
"generate_screen_from_text",
"get_screen",
];

console.log(`Allowed tools: ${allowedTools.join(", ")}`);

const tools = stitchTools({ include: allowedTools });

const google = createGoogleGenerativeAI();
const model = google("gemini-2.5-pro");

console.log("\nPrompting agent to generate a screen...");
const result = await generateText({
model,
tools,
prompt: "Generate a screen for a modern analytics dashboard. Please provide the ID of the screen you generated.",
maxSteps: 5,
});

console.log("\nAgent Response:");
console.log(result.text);

console.log("\nTool calls made by agent:");
for (const step of result.steps) {
for (const toolCall of step.toolCalls) {
console.log(`- ${toolCall.toolName}`);
}
}
}

main().catch(console.error);
Loading