@@ -2,8 +2,9 @@ import Anthropic from "@anthropic-ai/sdk";
22import OpenAI from "openai" ;
33import type { ChatModel } from "openai/resources" ;
44
5- const anthropic = new Anthropic ( ) ;
6- const openai = new OpenAI ( ) ;
5+ // Only initialize clients if API keys are available
6+ const anthropic = process . env . ANTHROPIC_API_KEY ? new Anthropic ( ) : null ;
7+ const openai = process . env . OPENAI_API_KEY ? new OpenAI ( ) : null ;
78
89export type AnthropicModel = Anthropic . Model ;
910export type OpenAIModel = ChatModel ;
@@ -25,6 +26,11 @@ export class McpServer {
2526 authorizationToken,
2627 name,
2728 type,
29+ } : {
30+ url : string ;
31+ authorizationToken : string ;
32+ name : string ;
33+ type : string ;
2834 } ) {
2935 this . url = url ;
3036 this . authorizationToken = authorizationToken ;
@@ -33,7 +39,6 @@ export class McpServer {
3339 }
3440}
3541
36-
3742export class Agents {
3843 private promptText : string = "" ;
3944 private allowedTools : string [ ] = [ ] ;
@@ -57,14 +62,13 @@ export class Agents {
5762 return this ;
5863 }
5964
60-
6165 async execute ( ) : Promise < this> {
6266 if ( ! this . mcpServer ) {
63- throw new Error ( ' MCP server not set' ) ;
67+ throw new Error ( " MCP server not set" ) ;
6468 }
6569
66- this . executionPromises = this . models . map ( async model => {
67- if ( model . startsWith ( ' claude' ) || model . startsWith ( ' claude-' ) ) {
70+ this . executionPromises = this . models . map ( async ( model ) => {
71+ if ( model . startsWith ( " claude" ) || model . startsWith ( " claude-" ) ) {
6872 return this . executeAnthropic ( model as AnthropicModel ) ;
6973 } else {
7074 return this . executeOpenAi ( model as OpenAIModel ) ;
@@ -80,7 +84,13 @@ export class Agents {
8084
8185 private async executeAnthropic ( model : AnthropicModel ) : Promise < this> {
8286 if ( ! this . mcpServer ) {
83- throw new Error ( 'MCP server not set' ) ;
87+ throw new Error ( "MCP server not set" ) ;
88+ }
89+
90+ if ( ! anthropic ) {
91+ throw new Error (
92+ "Anthropic client not initialized. Please set ANTHROPIC_API_KEY environment variable." ,
93+ ) ;
8494 }
8595
8696 const stream = anthropic . beta . messages . stream ( {
@@ -107,10 +117,16 @@ export class Agents {
107117 const usedTools : string [ ] = [ ] ;
108118
109119 for await ( const chunk of stream ) {
110- if ( chunk . type === "content_block_delta" && chunk . delta . type === "text_delta" ) {
120+ if (
121+ chunk . type === "content_block_delta" &&
122+ chunk . delta . type === "text_delta"
123+ ) {
111124 content += chunk . delta . text ;
112125 process . stdout . write ( chunk . delta . text ) ;
113- } else if ( chunk . type === "content_block_start" && chunk . content_block . type === "mcp_tool_use" ) {
126+ } else if (
127+ chunk . type === "content_block_start" &&
128+ chunk . content_block . type === "mcp_tool_use"
129+ ) {
114130 usedTools . push ( chunk . content_block . name ) ;
115131 }
116132 }
@@ -123,7 +139,13 @@ export class Agents {
123139
124140 private async executeOpenAi ( model : OpenAIModel ) : Promise < this> {
125141 if ( ! this . mcpServer ) {
126- throw new Error ( 'MCP server not set' ) ;
142+ throw new Error ( "MCP server not set" ) ;
143+ }
144+
145+ if ( ! openai ) {
146+ throw new Error (
147+ "OpenAI client not initialized. Please set OPENAI_API_KEY environment variable." ,
148+ ) ;
127149 }
128150
129151 const response = await openai . responses . create ( {
@@ -135,9 +157,9 @@ export class Agents {
135157 server_label : this . mcpServer . name ,
136158 server_url : this . mcpServer . url ,
137159 headers : {
138- " Authorization" : `Bearer ${ this . mcpServer . authorizationToken } `
139- }
140- }
160+ Authorization : `${ this . mcpServer . authorizationToken } ` ,
161+ } ,
162+ } ,
141163 ] ,
142164 input : this . promptText ,
143165 stream : true ,
@@ -146,9 +168,9 @@ export class Agents {
146168 const usedTools : string [ ] = [ ] ;
147169
148170 for await ( const chunk of response ) {
149- if ( chunk . type === ' response.output_item.added' ) {
171+ if ( chunk . type === " response.output_item.added" ) {
150172 const item = chunk . item ;
151- if ( item . type === ' mcp_call' ) {
173+ if ( item . type === " mcp_call" ) {
152174 usedTools . push ( item . name ) ;
153175 }
154176 }
@@ -159,4 +181,3 @@ export class Agents {
159181 return this ;
160182 }
161183}
162-
0 commit comments