-
-
Save Aakash67/02ce38b2d62d37450b733d6d7543b2c2 to your computer and use it in GitHub Desktop.
Trello agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // β STEP 1: Environment Setup | |
| // Make sure you have a .env file in your project root with this line: | |
| // | |
| // OPENAI_API_KEY=sk-your-openai-api-key | |
| // | |
| // Do NOT hardcode your API key in this file. Use dotenv to load it securely. | |
| import { ChatOpenAI } from "@langchain/openai"; | |
| import { createReactAgent } from "@langchain/langgraph/prebuilt"; | |
| import { HumanMessage, AIMessage } from "@langchain/core/messages"; | |
| import dotenv from "dotenv"; | |
| import * as readline from "node:readline/promises"; | |
| import { stdin as input, stdout as output } from "node:process"; | |
| import { MultiServerMCPClient } from "@langchain/mcp-adapters"; | |
| // Load variables from .env file | |
| dotenv.config(); | |
| // β STEP 2: Add your Trello MCP URL | |
| // Go to https://mcp.composio.dev, connect Trello, click "Cursor", and copy your tool URL. | |
| // Replace the placeholder below with your actual Trello tool URL. | |
| const mcpServers = { | |
| trello: { | |
| transport: "sse", | |
| url: "https://mcp.composio.dev/trello/YOUR-TRELLO-ID" | |
| } | |
| }; | |
| async function runChat() { | |
| const rl = readline.createInterface({ input, output }); | |
| const chatHistory = []; | |
| // Initialize MCP client and load tools | |
| const client = new MultiServerMCPClient(mcpServers); | |
| const tools = await client.getTools(); | |
| // β STEP 3: Load GPT-4o via OpenAI | |
| const model = new ChatOpenAI({ | |
| modelName: "gpt-4o", | |
| temperature: 0, | |
| openAIApiKey: process.env.OPENAI_API_KEY | |
| }); | |
| // Create the LangChain agent using the tools | |
| const agent = createReactAgent({ llm: model, tools }); | |
| console.log("Agent is ready. Type your message or 'exit' to quit.\n"); | |
| // π Main chat loop | |
| while (true) { | |
| const userInput = await rl.question("You: "); | |
| if (!userInput.trim()) continue; | |
| if (userInput.toLowerCase() === "exit") break; | |
| chatHistory.push(new HumanMessage(userInput)); | |
| console.log("\nAgent thinking...\n"); | |
| try { | |
| const result = await agent.invoke({ messages: chatHistory }); | |
| const lastMsg = result.messages[result.messages.length - 1]; | |
| const reply = typeof lastMsg.content === "string" | |
| ? lastMsg.content | |
| : JSON.stringify(lastMsg.content); | |
| console.log("Agent:", reply, "\n"); | |
| chatHistory.push(new AIMessage(reply)); | |
| } catch (err) { | |
| console.error("Agent error:", err.message); | |
| } | |
| } | |
| rl.close(); | |
| if (client?.close) await client.close(); | |
| console.log("Chat ended."); | |
| } | |
| runChat(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment