Skip to content

Commit 73c6a47

Browse files
add prompts endpoint
1 parent ab2680c commit 73c6a47

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

stagehand/src/index.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
CallToolResult,
99
Tool,
1010
ListResourcesRequestSchema,
11-
ListResourceTemplatesRequestSchema
11+
ListResourceTemplatesRequestSchema,
12+
ListPromptsRequestSchema,
13+
GetPromptRequestSchema
1214
} from "@modelcontextprotocol/sdk/types.js";
1315

1416
import { Stagehand } from "@browserbasehq/stagehand";
@@ -301,6 +303,15 @@ const TOOLS: Tool[] = [
301303
},
302304
];
303305

306+
// Define the prompts
307+
const PROMPTS = [
308+
{
309+
name: "click_search_button",
310+
description: "A prompt template for clicking on a search button",
311+
arguments: [] // No arguments required for this specific prompt
312+
}
313+
];
314+
304315
// Global state
305316
let stagehand: Stagehand | undefined;
306317
let serverInstance: Server | undefined;
@@ -649,6 +660,7 @@ const server = new Server(
649660
resources: {},
650661
tools: {},
651662
logging: {},
663+
prompts: {}
652664
},
653665
}
654666
);
@@ -742,6 +754,61 @@ server.setRequestHandler(ListResourceTemplatesRequestSchema, async (request) =>
742754
}
743755
});
744756

757+
server.setRequestHandler(ListPromptsRequestSchema, async (request) => {
758+
try {
759+
logRequest('ListPrompts', request.params);
760+
const response = { prompts: PROMPTS };
761+
const sanitizedResponse = sanitizeMessage(response);
762+
logResponse('ListPrompts', JSON.parse(sanitizedResponse));
763+
return JSON.parse(sanitizedResponse);
764+
} catch (error) {
765+
const errorMsg = error instanceof Error ? error.message : String(error);
766+
return {
767+
error: {
768+
code: -32603,
769+
message: `Internal error: ${errorMsg}`,
770+
},
771+
};
772+
}
773+
});
774+
775+
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
776+
try {
777+
logRequest('GetPrompt', request.params);
778+
779+
// Check if prompt name is valid
780+
if (request.params?.name !== "click_search_button") {
781+
throw new Error(`Invalid prompt name: ${request.params?.name}`);
782+
}
783+
784+
// Return a prompt for clicking on a search button
785+
const response = {
786+
description: "This prompt provides instructions for clicking on a search button",
787+
messages: [
788+
{
789+
role: "user",
790+
content: {
791+
type: "text",
792+
text: "Please click on the search button"
793+
}
794+
}
795+
]
796+
};
797+
798+
const sanitizedResponse = sanitizeMessage(response);
799+
logResponse('GetPrompt', JSON.parse(sanitizedResponse));
800+
return JSON.parse(sanitizedResponse);
801+
} catch (error) {
802+
const errorMsg = error instanceof Error ? error.message : String(error);
803+
return {
804+
error: {
805+
code: -32603,
806+
message: `Internal error: ${errorMsg}`,
807+
},
808+
};
809+
}
810+
});
811+
745812
// Run the server
746813
async function runServer() {
747814
const transport = new StdioServerTransport();

0 commit comments

Comments
 (0)