Skip to content

Commit 28b9307

Browse files
committed
refactor: move minimal example to cli agent
1 parent ae64b20 commit 28b9307

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

packages/agent-api/src/minimal.ts renamed to packages/agent-api/cli-agent.ts

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,77 @@
11
import { AzureChatOpenAI } from '@langchain/openai';
22
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
33
import { ChatPromptTemplate } from '@langchain/core/prompts';
4-
import { createToolCallingAgent } from "langchain/agents";
5-
import { AgentExecutor } from "langchain/agents";
6-
import { loadMcpTools } from "@langchain/mcp-adapters";
7-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
8-
import 'dotenv/config';
9-
import { getAzureOpenAiTokenProvider, getCredentials, getUserId } from './auth.js';
4+
import { createToolCallingAgent } from 'langchain/agents';
5+
import { AgentExecutor } from 'langchain/agents';
6+
import { loadMcpTools } from '@langchain/mcp-adapters';
7+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
8+
import { getAzureOpenAiTokenProvider } from './src/auth.js';
109
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
10+
import path from 'node:path';
11+
import dotenv from 'dotenv';
12+
13+
dotenv.config({ path: path.join(process.cwd(), '../../.env') });
1114

1215
const agentSystemPrompt = `
1316
# Role
1417
You an expert assistant that helps users with managing burger orders. Use the provided tools to get the information you need and perform actions on behalf of the user.
15-
Only anwser to requests that are related to burger orders and the menu. If the user asks for something else, politely inform them that you can only assist with burger orders.
18+
Only answer to requests that are related to burger orders and the menu. If the user asks for something else, politely inform them that you can only assist with burger orders.
1619
1720
# Task
18-
1. Help the user with their request, ask any clarifying questions if needed.
19-
2. ALWAYS generate 3 very brief follow-up questions that the user would likely ask next.
20-
Enclose the follow-up questions in double angle brackets. Example:
21-
<<Am I allowed to invite friends for a party?>>
22-
<<How can I ask for a refund?>>
23-
<<What If I break something?>>
24-
Make sure the last question ends with ">>".
21+
Help the user with their request, ask any clarifying questions if needed.
2522
2623
# Instructions
2724
- Always use the tools provided to get the information requested or perform any actions
2825
- If you get any errors when trying to use a tool that does not seem related to missing parameters, try again
2926
- If you cannot get the information needed to answer the user's question or perform the specified action, inform the user that you are unable to do so. Never make up information.
3027
- The get_burger tool can help you get informations about the burgers
28+
- Creating or cancelling an order requires a \`userId\`: if not provided, ask the user to provide it or to run the CLI with the \`--userId\` option.
3129
`;
3230

33-
const question = "show me the burger menu";
31+
const question = 'show me the burger menu';
3432

3533
export async function run() {
3634
const azureOpenAiEndpoint = process.env.AZURE_OPENAI_API_ENDPOINT;
37-
const burgerMcpEndpoint = 'http://localhost:3000/mcp' //process.env.BURGER_MCP_ENDPOINT;
35+
const burgerMcpEndpoint = process.env.BURGER_MCP_URL ?? 'http://localhost:3000/mcp';
3836

3937
try {
4038
let model: BaseChatModel;
4139

4240
if (!azureOpenAiEndpoint || !burgerMcpEndpoint) {
43-
const errorMessage = 'Missing required environment variables: AZURE_OPENAI_API_ENDPOINT or BURGER_MCP_ENDPOINT';
41+
const errorMessage = 'Missing required environment variables: AZURE_OPENAI_API_ENDPOINT or BURGER_MCP_URL';
4442
console.error(errorMessage);
4543
return {
4644
status: 500,
4745
jsonBody: {
4846
error: errorMessage,
4947
},
50-
}
48+
};
5149
}
5250

5351
const azureADTokenProvider = getAzureOpenAiTokenProvider();
5452

5553
model = new AzureChatOpenAI({
5654
// Controls randomness. 0 = deterministic, 1 = maximum randomness
57-
temperature: 0.7,
55+
temperature: 0.3,
5856
azureADTokenProvider,
5957
});
6058

6159
const client = new Client({
6260
name: 'burger-mcp',
63-
version: '1.0.0'
61+
version: '1.0.0',
6462
});
6563
const transport = new StreamableHTTPClientTransport(new URL(burgerMcpEndpoint));
6664
await client.connect(transport);
67-
console.log("Connected to Burger MCP server using Streamable HTTP transport");
68-
69-
const tools = await loadMcpTools("burger", client);
65+
console.log(`Connected to Burger MCP server at ${burgerMcpEndpoint}`);
7066

71-
const toolDefinitions = await client.listTools();
72-
console.log(JSON.stringify(toolDefinitions, null, 2));
73-
74-
// for (const tool of tools) {
75-
// if (!(tool.schema as any).properties) {
76-
// (tool as any).schema = undefined;
77-
// }
78-
// }
67+
const tools = await loadMcpTools('burger', client);
7968
console.log(`Loaded ${tools.length} tools from Burger MCP server`);
8069

8170
const prompt = ChatPromptTemplate.fromMessages([
82-
["system", agentSystemPrompt],
83-
["placeholder", "{chat_history}"],
84-
["human", "{input}"],
85-
["placeholder", "{agent_scratchpad}"],
71+
['system', agentSystemPrompt],
72+
['placeholder', '{chat_history}'],
73+
['human', '{input}'],
74+
['placeholder', '{agent_scratchpad}'],
8675
]);
8776

8877
const agent = createToolCallingAgent({
@@ -94,17 +83,12 @@ console.log(JSON.stringify(toolDefinitions, null, 2));
9483
agent,
9584
tools,
9685
returnIntermediateSteps: true,
97-
// verbose: true,
9886
});
9987

100-
const response = await agentExecutor.invoke(
101-
{ input: question },
102-
);
103-
104-
// console.log("Response:", response);
105-
console.log("Intermediate steps:", JSON.stringify(response.intermediateSteps, null, 2));
106-
console.log("Final answer:", response.output);
88+
const response = await agentExecutor.invoke({ input: question });
10789

90+
console.log('Intermediate steps:', JSON.stringify(response.intermediateSteps, null, 2));
91+
console.log('Final answer:', response.output);
10892
} catch (_error: unknown) {
10993
const error = _error as Error;
11094
console.error(`Error when processing chat-post request: ${error.message}`);

0 commit comments

Comments
 (0)