Skip to main content
Glama
devashishkumar

OpenRouter LangChain MCP Server

README.md
# OpenRouter LangChain MCP Server

A small command-line chatbot that lets an OpenRouter model query MongoDB through a Model Context Protocol (MCP) server.

The project has two processes:

- `server.js` exposes MongoDB query tools over MCP stdio transport.
- `client.js` connects to the server, binds its tools to a LangChain OpenRouter model, and provides an interactive chat prompt.

## Requirements

- Node.js 18 or newer
- A running MongoDB instance
- An [OpenRouter API key](https://openrouter.ai/keys)

## Setup

1. Install dependencies:

	```bash
	npm install
	```

2. Create a `.env` file in the project root:

	```env
	OPENROUTER_API_KEY=your_openrouter_api_key
	MONGODB_URI=mongodb://localhost:27017
	```

	`MONGODB_URI` is optional and defaults to `mongodb://localhost:27017`.

3. Start the interactive client:

	```bash
	npm start
	```

The client starts `server.js` automatically over stdio, so the MCP server does not need to be started separately.

## First run

The server uses the `chatbot_db` database. If both the `users` and `orders` collections are missing, it creates them and inserts sample records. Existing collections are left unchanged.

The sample data includes:

- `users`: user IDs, names, email addresses, and statuses
- `orders`: order IDs, user IDs, products, and amounts

## Available MCP tools

### `get_collections`

Lists the collections in the configured MongoDB database.

### `query_collection`

Runs a MongoDB `find` query. It accepts:

- `collection` (required): collection name, such as `users` or `orders`
- `filter` (optional): MongoDB filter object
- `limit` (optional): maximum number of documents to return; defaults to `10`

Examples of questions to ask the chatbot:

```text
Which users are active?
Show orders over $100.
What products did user 1 order?
List the available collections.
```

Type `exit` to close the client.

## Configuration

The client currently uses the OpenRouter model identifier:

```text
nvidia/nemotron-3-ultra-550b-a55b:free
```

To use another OpenRouter-supported model, change the `model` value in `client.js`.

## Project structure

```text
.
├── client.js   # Interactive LangChain client
├── server.js   # MCP server and MongoDB tools
├── package.json
└── README.md
```

## Notes

- Keep `.env` out of version control because it contains your API key.
- The MCP server logs connection and seeding messages to stderr so they do not interfere with the stdio protocol.
- The `npm start` script runs `client.js`, which starts the MCP server automatically over stdio.

## Chatbot

![Chatbot](assets/consoleapp/chatbot1.png)
![Chatbot](assets/consoleapp/chatbot2.png)

## Users Collection

![Chatbot](assets/consoleapp/users-collection.png)

## Orders Collection

![Chatbot](assets/consoleapp/orders-collection.png)


## Web application

The `webapp/` directory contains a browser-based version of the chatbot. It
runs an Express server, serves the UI from `webapp/public/index.html`, and
exposes the chat endpoint at `POST /api/chat`.

The webapp runs the MCP server and client in the same Node.js process using an
in-memory transport. The browser can therefore use the same MongoDB tools as
the command-line client without starting a separate MCP process.

### Run the webapp

1. Install the webapp dependencies:

	```bash
	cd webapp
	npm install
	```

2. Create a `.env` file in the `webapp/` directory:

	```env
	OPENROUTER_API_KEY=your_openrouter_api_key
	MONGODB_URI=mongodb://localhost:27017
	PORT=3000
	```

	`MONGODB_URI` defaults to `mongodb://localhost:27017`, and `PORT` defaults
	to `3000`.

3. Start the server:

	```bash
	node server.js
	```

4. Open [http://localhost:3000](http://localhost:3000) in a browser.

### Webapp behavior

- The UI accepts natural-language questions about the MongoDB database.
- Tool calls made by the model are displayed in the conversation.
- The final model response is displayed after the tool calls complete.
- Press `Enter` to send a message, or use the **Send** button.
- The API returns `{ reply, steps }` for successful requests and `{ error }`
  for failures.

Example request:

```bash
curl -X POST http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"message":"Show me active users"}'
```

### Webapp project structure

```text
webapp/
├── public/
│   └── index.html   # Browser chat interface
├── package.json
└── server.js        # Express app, MCP tools, and chat API
```

The webapp uses the same `chatbot_db` database and sample `users` and
`orders` collections described above. Keep the `.env` file out of version
control because it contains the OpenRouter API key.

## Webapp

![Chatbot](assets/webapp/server.png)
![Chatbot](assets/webapp/webapp.png)