@roarkanalytics/sdk-mcp 2.23.0 → 2.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/server.ts CHANGED
@@ -11,52 +11,16 @@ import { ClientOptions } from '@roarkanalytics/sdk';
11
11
  import Roark from '@roarkanalytics/sdk';
12
12
  import { codeTool } from './code-tool';
13
13
  import docsSearchTool from './docs-search-tool';
14
+ import { getInstructions } from './instructions';
14
15
  import { McpOptions } from './options';
15
16
  import { blockedMethodsForCodeTool } from './methods';
16
17
  import { HandlerFunction, McpRequestContext, ToolCallResult, McpTool } from './types';
17
- import { readEnv } from './util';
18
-
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.
22
- const response = await fetch(
23
- readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/roark-analytics',
24
- {
25
- method: 'GET',
26
- headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
27
- },
28
- );
29
-
30
- let instructions: string | undefined;
31
- if (!response.ok) {
32
- console.warn(
33
- 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
34
- );
35
-
36
- instructions = `
37
- This is the roark-analytics MCP server. You will use Code Mode to help the user perform
38
- actions. You can use search_docs tool to learn about how to take action with this server. Then,
39
- you will write TypeScript code using the execute tool take action. It is CRITICAL that you be
40
- thoughtful and deliberate when executing code. Always try to entirely solve the problem in code
41
- block: it can be as long as you need to get the job done!
42
- `;
43
- }
44
-
45
- instructions ??= ((await response.json()) as { instructions: string }).instructions;
46
- instructions = `
47
- The current time in Unix timestamps is ${Date.now()}.
48
-
49
- ${instructions}
50
- `;
51
-
52
- return instructions;
53
- }
54
18
 
55
19
  export const newMcpServer = async (stainlessApiKey: string | undefined) =>
56
20
  new McpServer(
57
21
  {
58
22
  name: 'roarkanalytics_sdk_api',
59
- version: '2.23.0',
23
+ version: '2.24.0',
60
24
  },
61
25
  {
62
26
  instructions: await getInstructions(stainlessApiKey),
@@ -91,14 +55,32 @@ export async function initMcpServer(params: {
91
55
  error: logAtLevel('error'),
92
56
  };
93
57
 
94
- let client = new Roark({
95
- logger,
96
- ...params.clientOptions,
97
- defaultHeaders: {
98
- ...params.clientOptions?.defaultHeaders,
99
- 'X-Stainless-MCP': 'true',
100
- },
101
- });
58
+ let _client: Roark | undefined;
59
+ let _clientError: Error | undefined;
60
+ let _logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off' | undefined;
61
+
62
+ const getClient = (): Roark => {
63
+ if (_clientError) throw _clientError;
64
+ if (!_client) {
65
+ try {
66
+ _client = new Roark({
67
+ logger,
68
+ ...params.clientOptions,
69
+ defaultHeaders: {
70
+ ...params.clientOptions?.defaultHeaders,
71
+ 'X-Stainless-MCP': 'true',
72
+ },
73
+ });
74
+ if (_logLevel) {
75
+ _client = _client.withOptions({ logLevel: _logLevel });
76
+ }
77
+ } catch (e) {
78
+ _clientError = e instanceof Error ? e : new Error(String(e));
79
+ throw _clientError;
80
+ }
81
+ }
82
+ return _client;
83
+ };
102
84
 
103
85
  const providedTools = selectTools(params.mcpOptions);
104
86
  const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
@@ -116,6 +98,21 @@ export async function initMcpServer(params: {
116
98
  throw new Error(`Unknown tool: ${name}`);
117
99
  }
118
100
 
101
+ let client: Roark;
102
+ try {
103
+ client = getClient();
104
+ } catch (error) {
105
+ return {
106
+ content: [
107
+ {
108
+ type: 'text' as const,
109
+ text: `Failed to initialize client: ${error instanceof Error ? error.message : String(error)}`,
110
+ },
111
+ ],
112
+ isError: true,
113
+ };
114
+ }
115
+
119
116
  return executeHandler({
120
117
  handler: mcpTool.handler,
121
118
  reqContext: {
@@ -128,24 +125,29 @@ export async function initMcpServer(params: {
128
125
 
129
126
  server.setRequestHandler(SetLevelRequestSchema, async (request) => {
130
127
  const { level } = request.params;
128
+ let logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off';
131
129
  switch (level) {
132
130
  case 'debug':
133
- client = client.withOptions({ logLevel: 'debug' });
131
+ logLevel = 'debug';
134
132
  break;
135
133
  case 'info':
136
- client = client.withOptions({ logLevel: 'info' });
134
+ logLevel = 'info';
137
135
  break;
138
136
  case 'notice':
139
137
  case 'warning':
140
- client = client.withOptions({ logLevel: 'warn' });
138
+ logLevel = 'warn';
141
139
  break;
142
140
  case 'error':
143
- client = client.withOptions({ logLevel: 'error' });
141
+ logLevel = 'error';
144
142
  break;
145
143
  default:
146
- client = client.withOptions({ logLevel: 'off' });
144
+ logLevel = 'off';
147
145
  break;
148
146
  }
147
+ _logLevel = logLevel;
148
+ if (_client) {
149
+ _client = _client.withOptions({ logLevel });
150
+ }
149
151
  return {};
150
152
  });
151
153
  }