@@ -13,17 +13,17 @@ import { codeTool } from './code-tool';
1313import docsSearchTool from './docs-search-tool' ;
1414import { McpOptions } from './options' ;
1515import { blockedMethodsForCodeTool } from './methods' ;
16- import { HandlerFunction , McpTool } from './types' ;
16+ import { HandlerFunction , McpRequestContext , ToolCallResult , McpTool } from './types' ;
1717import { readEnv } from './util' ;
1818
19- async function getInstructions ( ) {
20- // This API key is optional; providing it allows the server to fetch instructions for unreleased versions.
21- const stainlessAPIKey = readEnv ( 'STAINLESS_API_KEY' ) ;
19+ async function getInstructions ( stainlessApiKey : string | undefined ) : Promise < string > {
20+ // Setting the stainless API key is optional, but may be required
21+ // to authenticate requests to the Stainless API.
2222 const response = await fetch (
2323 readEnv ( 'CODE_MODE_INSTRUCTIONS_URL' ) ?? 'https://api.stainless.com/api/ai/instructions/finch' ,
2424 {
2525 method : 'GET' ,
26- headers : { ...( stainlessAPIKey && { Authorization : stainlessAPIKey } ) } ,
26+ headers : { ...( stainlessApiKey && { Authorization : stainlessApiKey } ) } ,
2727 } ,
2828 ) ;
2929
@@ -52,14 +52,14 @@ async function getInstructions() {
5252 return instructions ;
5353}
5454
55- export const newMcpServer = async ( ) =>
55+ export const newMcpServer = async ( stainlessApiKey : string | undefined ) =>
5656 new McpServer (
5757 {
5858 name : 'tryfinch_finch_api_api' ,
5959 version : '9.0.0' ,
6060 } ,
6161 {
62- instructions : await getInstructions ( ) ,
62+ instructions : await getInstructions ( stainlessApiKey ) ,
6363 capabilities : { tools : { } , logging : { } } ,
6464 } ,
6565 ) ;
@@ -72,6 +72,7 @@ export async function initMcpServer(params: {
7272 server : Server | McpServer ;
7373 clientOptions ?: ClientOptions ;
7474 mcpOptions ?: McpOptions ;
75+ stainlessApiKey ?: string | undefined ;
7576} ) {
7677 const server = params . server instanceof McpServer ? params . server . server : params . server ;
7778
@@ -116,7 +117,14 @@ export async function initMcpServer(params: {
116117 throw new Error ( `Unknown tool: ${ name } ` ) ;
117118 }
118119
119- return executeHandler ( mcpTool . handler , client , args ) ;
120+ return executeHandler ( {
121+ handler : mcpTool . handler ,
122+ reqContext : {
123+ client,
124+ stainlessApiKey : params . stainlessApiKey ?? params . mcpOptions ?. stainlessApiKey ,
125+ } ,
126+ args,
127+ } ) ;
120128 } ) ;
121129
122130 server . setRequestHandler ( SetLevelRequestSchema , async ( request ) => {
@@ -161,10 +169,14 @@ export function selectTools(options?: McpOptions): McpTool[] {
161169/**
162170 * Runs the provided handler with the given client and arguments.
163171 */
164- export async function executeHandler (
165- handler : HandlerFunction ,
166- client : Finch ,
167- args : Record < string , unknown > | undefined ,
168- ) {
169- return await handler ( client , args || { } ) ;
172+ export async function executeHandler ( {
173+ handler,
174+ reqContext,
175+ args,
176+ } : {
177+ handler : HandlerFunction ;
178+ reqContext : McpRequestContext ;
179+ args : Record < string , unknown > | undefined ;
180+ } ) : Promise < ToolCallResult > {
181+ return await handler ( { reqContext, args : args || { } } ) ;
170182}
0 commit comments