|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +<img src="./packages/agent-webapp/public/favicon.png" alt="" align="center" height="64" /> |
| 4 | + |
| 5 | +# AI Agent with MCP tools using LangChain.js |
| 6 | + |
| 7 | +[](https://codespaces.new/Azure-Samples/mcp-agent-langchainjs?hide_repo_select=true&ref=main&quickstart=true) |
| 8 | +[](https://discord.gg/kzRShWzttr) |
| 9 | +<br> |
| 10 | +[](https://github.com/Azure-Samples/mcp-agent-langchainjs/actions) |
| 11 | + |
| 12 | +[](https://www.typescriptlang.org) |
| 13 | +[](LICENSE) |
| 14 | + |
| 15 | +⭐ If you like this sample, star it on GitHub — it helps a lot! |
| 16 | + |
| 17 | +[Overview](#overview) • [Architecture](#architecture) • [Getting started](#getting-started) • [Local development](#local-development) • [Deployment](#deployment) • [Resources](#resources) • [Troubleshooting](#troubleshooting) |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +</div> |
| 22 | + |
| 23 | +## Overview |
| 24 | + |
| 25 | +This sample shows how to build a serverless AI agent that can take real actions through an existing business API using the **Model Context Protocol (MCP)**. The demo scenario is a burger restaurant: users chat with an AI assistant to explore the menu and place orders. The agent uses **LangChain.js** to decide when to call MCP tools connected to a burger ordering API. |
| 26 | + |
| 27 | +The application is hosted on [Azure Static Web Apps](https://learn.microsoft.com/azure/static-web-apps/overview) (web apps) and [Azure Functions](https://learn.microsoft.com/azure/azure-functions/functions-overview?pivots=programming-language-javascript) (API and MCP servers), with [Azure Cosmos DB for NoSQL](https://learn.microsoft.com/azure/cosmos-db/nosql/) for data storage. You can use it as a starting point for building your own AI agents. |
| 28 | + |
| 29 | +### Key features |
| 30 | + |
| 31 | +- LangChain.js agent with tool calling via MCP (Streamable HTTP transport) |
| 32 | +- Multi-service, end‑to‑end architecture (web UIs, APIs, MCP server) |
| 33 | +- User authentication with sessions history |
| 34 | +- 100% serverless architecture, for cost-effective scaling |
| 35 | +- Single-command deployment using Infrastructure as Code (IaC) |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +Building AI applications can be complex and time-consuming, but using LangChain.js and Azure serverless technologies allows to greatly simplify the process. This application is a AI agent that can be access through different interfaces (web app, CLI) and that can call tools through MCP to interact with a burger ordering API. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +The application is made from these main components: |
| 44 | + |
| 45 | +| Component | Folder | Purpose | |
| 46 | +|-------|--------|---------| |
| 47 | +| Agent Web App | `packages/agent-webapp` | Chat interface + conversation rendering | |
| 48 | +| Agent API | `packages/agent-api` | LangChain.js agent + chat state + MCP client | |
| 49 | +| Burger API | `packages/burger-api` | Core burger & order management web API | |
| 50 | +| Burger MCP Server | `packages/burger-mcp` | Exposes burger API as MCP tools | |
| 51 | +| Burger Web App | `packages/burger-webapp` | Live orders visualization | |
| 52 | +| Infrastructure | `infra` | Bicep templates (IaC) | |
| 53 | + |
| 54 | +Additionally, these support components are included: |
| 55 | + |
| 56 | +| Component | Folder | Purpose | |
| 57 | +|-------|--------|---------| |
| 58 | +| Agent CLI | `packages/agent-cli` | Command-line interface LangChain.js agent and MCP client | |
| 59 | +| Data generation | `packages/burger-data` | Scripts to (re)generate burgers data & images | |
| 60 | + |
| 61 | +## Getting started |
| 62 | + |
| 63 | +### 1. Prerequisites |
| 64 | + |
| 65 | +- Node.js >= 22 / npm >= 10 |
| 66 | +- Git |
| 67 | +- (For full Azure deployment) Azure CLI, Azure Developer CLI (`azd`), Azure Functions Core Tools, Static Web Apps CLI |
| 68 | + |
| 69 | +### 2. Clone & install |
| 70 | + |
| 71 | +```bash |
| 72 | +git clone https://github.com/Azure-Samples/mcp-agent-langchainjs |
| 73 | +cd mcp-agent-langchainjs |
| 74 | +npm install |
| 75 | +``` |
| 76 | + |
| 77 | +### 3. Run all services locally |
| 78 | + |
| 79 | +```bash |
| 80 | +npm start |
| 81 | +``` |
| 82 | + |
| 83 | +When you see the consolidated readiness banner: |
| 84 | + |
| 85 | +``` |
| 86 | +- Agent webapp : http://localhost:4280 |
| 87 | +- Burger webapp : http://localhost:5173 |
| 88 | +- Burger MCP : http://localhost:3000/mcp |
| 89 | +``` |
| 90 | + |
| 91 | +Burger API (Functions) also runs locally (commonly on `http://localhost:7071`). Agent API runs on `http://localhost:7072`. |
| 92 | + |
| 93 | +> [!TIP] |
| 94 | +> Want a faster UI-only iteration loop? Start individual packages (e.g. `npm run start --workspace=agent-webapp`). |
| 95 | +
|
| 96 | +### 4. Chat & order |
| 97 | + |
| 98 | +Open the Agent webapp and ask things like: |
| 99 | + |
| 100 | +> What spicy burgers do you have? |
| 101 | +> Order two Classic Cheeseburgers with extra bacon. |
| 102 | +> Show my recent orders. |
| 103 | +
|
| 104 | +The agent will decide which MCP tool(s) to call; tool call results are streamed back into the conversation. |
| 105 | + |
| 106 | +## Local development |
| 107 | + |
| 108 | +Common scripts (run from repo root unless noted): |
| 109 | + |
| 110 | +| Action | Command | |
| 111 | +|--------|---------| |
| 112 | +| Start everything | `npm start` | |
| 113 | +| Build all | `npm run build` | |
| 114 | +| Lint | `npm run lint` | |
| 115 | +| Fix lint | `npm run lint:fix` | |
| 116 | +| Format | `npm run format` | |
| 117 | + |
| 118 | +Per-package development (examples): |
| 119 | + |
| 120 | +```bash |
| 121 | +npm run start --workspace=burger-api # Burger API (Functions) |
| 122 | +npm run start --workspace=agent-api # Agent API (Functions) |
| 123 | +npm run start --workspace=burger-mcp # MCP server (HTTP /mcp & /sse) |
| 124 | +npm run start --workspace=agent-webapp # Agent chat UI |
| 125 | +npm run start --workspace=burger-webapp # Orders dashboard |
| 126 | +``` |
| 127 | + |
| 128 | +Use mock data where available: |
| 129 | + |
| 130 | +```bash |
| 131 | +npm run start:mock --workspace=agent-webapp |
| 132 | +npm run start:mock --workspace=burger-webapp |
| 133 | +``` |
| 134 | + |
| 135 | +### MCP inspection |
| 136 | + |
| 137 | +You can explore tools interactively: |
| 138 | + |
| 139 | +```bash |
| 140 | +npx -y @modelcontextprotocol/inspector |
| 141 | +``` |
| 142 | + |
| 143 | +Open the displayed local URL, choose Streamable HTTP, and connect to `http://localhost:3000/mcp` then list & invoke tools. |
| 144 | + |
| 145 | +> [!NOTE] |
| 146 | +> An SSE endpoint is also available at `/sse` for backward compatibility. |
| 147 | +
|
| 148 | +## Deployment |
| 149 | + |
| 150 | +Provision & deploy all Azure resources using **Azure Developer CLI**: |
| 151 | + |
| 152 | +```bash |
| 153 | +azd auth login |
| 154 | +azd up # (provisions + deploys) or split: azd provision && azd deploy |
| 155 | +``` |
| 156 | + |
| 157 | +After provisioning, hooks populate a root `.env` file (`azd env get-values > .env`) and `npm run env` prints resolved service URLs. |
| 158 | + |
| 159 | +Subsequent incremental deploys: |
| 160 | + |
| 161 | +```bash |
| 162 | +azd deploy agent-api |
| 163 | +azd deploy burger-api |
| 164 | +``` |
| 165 | + |
| 166 | +> [!TIP] |
| 167 | +> The Static Web Apps services build automatically; the `burger-webapp` predeploy hook ensures `BURGER_API_URL` is set. |
| 168 | +
|
| 169 | +## MCP tools |
| 170 | + |
| 171 | +Provided by `burger-mcp`: |
| 172 | + |
| 173 | +| Tool | Description | |
| 174 | +|------|-------------| |
| 175 | +| `get_burgers` | List burgers | |
| 176 | +| `get_burger_by_id` | Fetch a burger | |
| 177 | +| `get_toppings` | List toppings (filterable) | |
| 178 | +| `get_topping_by_id` | Fetch a topping | |
| 179 | +| `get_topping_categories` | List topping categories | |
| 180 | +| `get_orders` | List orders (user / status filters) | |
| 181 | +| `get_order_by_id` | Fetch order details | |
| 182 | +| `place_order` | Create an order (requires user context) | |
| 183 | +| `delete_order_by_id` | Cancel pending order | |
| 184 | + |
| 185 | +These map directly to REST endpoints on `burger-api`. The agent selects them via tool-calling decisions. |
| 186 | + |
| 187 | + |
| 188 | +## Troubleshooting |
| 189 | + |
| 190 | +| Symptom | Possible cause / fix | |
| 191 | +|---------|----------------------| |
| 192 | +| Agent can’t list burgers | MCP server not started or wrong `BURGER_MCP_URL` / local port mismatch | |
| 193 | +| Tool calls hang | Check MCP server logs; ensure Streamable HTTP endpoint `/mcp` reachable | |
| 194 | +| Orders never advance status | Timer function not running (Functions host not active) | |
| 195 | +| Missing images | Blob storage not provisioned yet; fallback data may omit images | |
| 196 | +| Auth issues in SWA | Ensure you’re running via `swa start` (agent-webapp start script) | |
| 197 | + |
| 198 | +> [!TIP] |
| 199 | +> Use the MCP Inspector to isolate whether issues lie in the agent’s reasoning layer or underlying tools. |
0 commit comments