Skip to content

Commit 5f74578

Browse files
feat(mcp): add initial server instructions
Adds generated MCP server instructions, to help agents get easy tasks on the first try.
1 parent c5a7d72 commit 5f74578

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

packages/mcp-server/src/http.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import { McpOptions } from './options';
99
import { ClientOptions, initMcpServer, newMcpServer } from './server';
1010
import { parseAuthHeaders } from './headers';
1111

12-
const newServer = ({
12+
const newServer = async ({
1313
clientOptions,
1414
req,
1515
res,
1616
}: {
1717
clientOptions: ClientOptions;
1818
req: express.Request;
1919
res: express.Response;
20-
}): McpServer | null => {
21-
const server = newMcpServer();
20+
}): Promise<McpServer | null> => {
21+
const server = await newMcpServer();
2222

2323
try {
2424
const authOptions = parseAuthHeaders(req, false);
25-
initMcpServer({
25+
await initMcpServer({
2626
server: server,
2727
clientOptions: {
2828
...clientOptions,
@@ -46,7 +46,7 @@ const newServer = ({
4646
const post =
4747
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
4848
async (req: express.Request, res: express.Response) => {
49-
const server = newServer({ ...options, req, res });
49+
const server = await newServer({ ...options, req, res });
5050
// If we return null, we already set the authorization error.
5151
if (server === null) return;
5252
const transport = new StreamableHTTPServerTransport();

packages/mcp-server/src/server.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,59 @@ import { HandlerFunction, McpTool } from './types';
1717
export { McpOptions } from './options';
1818
export { ClientOptions } from 'conductor-node';
1919

20-
export const newMcpServer = () =>
20+
async function getInstructions() {
21+
// This API key is optional; providing it allows the server to fetch instructions for unreleased versions.
22+
const stainlessAPIKey = readEnv('STAINLESS_API_KEY');
23+
const response = await fetch(
24+
readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/conductor',
25+
{
26+
method: 'GET',
27+
headers: { ...(stainlessAPIKey && { Authorization: stainlessAPIKey }) },
28+
},
29+
);
30+
31+
let instructions: string | undefined;
32+
if (!response.ok) {
33+
console.warn(
34+
'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
35+
);
36+
37+
instructions = `
38+
This is the conductor MCP server. You will use Code Mode to help the user perform
39+
actions. You can use search_docs tool to learn about how to take action with this server. Then,
40+
you will write TypeScript code using the execute tool take action. It is CRITICAL that you be
41+
thoughtful and deliberate when executing code. Always try to entirely solve the problem in code
42+
block: it can be as long as you need to get the job done!
43+
`;
44+
}
45+
46+
instructions ??= ((await response.json()) as { instructions: string }).instructions;
47+
instructions = `
48+
The current time in Unix timestamps is ${Date.now()}.
49+
50+
${instructions}
51+
`;
52+
53+
return instructions;
54+
}
55+
56+
export const newMcpServer = async () =>
2157
new McpServer(
2258
{
2359
name: 'conductor_node_api',
2460
version: '14.5.0',
2561
},
26-
{ capabilities: { tools: {}, logging: {} } },
62+
{
63+
instructions: await getInstructions(),
64+
capabilities: { tools: {}, logging: {} },
65+
},
2766
);
2867

29-
// Create server instance
30-
export const server = newMcpServer();
31-
3268
/**
3369
* Initializes the provided MCP Server with the given tools and handlers.
3470
* If not provided, the default client, tools and handlers will be used.
3571
*/
36-
export function initMcpServer(params: {
72+
export async function initMcpServer(params: {
3773
server: Server | McpServer;
3874
clientOptions?: ClientOptions;
3975
mcpOptions?: McpOptions;

packages/mcp-server/src/stdio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
22
import { initMcpServer, newMcpServer } from './server';
33

44
export const launchStdioServer = async () => {
5-
const server = newMcpServer();
5+
const server = await newMcpServer();
66

7-
initMcpServer({ server });
7+
await initMcpServer({ server });
88

99
const transport = new StdioServerTransport();
1010
await server.connect(transport);

0 commit comments

Comments
 (0)