Simple MCP Client with LangGraph Agent

by simozampa

335 downloads Not rated yet

About

Simple Multi-Server MCP Client with LangChain

Explore

- Express.js server with TypeScript
- LangGraph agent for handling conversations
- MCP client for tool integration
- Streaming responses

1. Clone the repository
2. Install dependencies:
npm install
3. Create a .env file with the following variables:

   PORT=3000
NODE_ENV=development
CORS_ORIGIN=*
ANTHROPIC_API_KEY=your_anthropic_api_key

4. Build and start the server:
npm run buildnpm start

// Send a message and stream the response
const response = await fetch("/api/chat", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    input: "What's the weather in New York?",
    characterId: "agent1",
    chatHistory: [],
    mcpServers: [
      {
        name: "weather-service",
        version: "1.0",
        url: "https://my-mcp-server.com",
      },
    ],
  }),
});

// Read the streaming response
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = new TextDecoder().decode(value);
const events = text.split("\n\n").filter(Boolean);
for (const event of events) {
const data = JSON.parse(event);
console.log(data);
}
}

A lightweight implementation of a multiserver MCP (Model Context Protocol) client using a LangGraph agent with Express.js and TypeScript.

Features

- Express.js server with TypeScript
- LangGraph agent for handling conversations
- MCP client for tool integration
- Streaming responses

Setup

1. Clone the repository
2. Install dependencies:
npm install
3. Create a .env file with the following variables:

   PORT=3000
NODE_ENV=development
CORS_ORIGIN=*
ANTHROPIC_API_KEY=your_anthropic_api_key

4. Build and start the server:
npm run buildnpm start

API Endpoints

Initialize Chat

GET /api/chat/init

Generate Chat

POST /api/chat

Request body:

{
  "input": "Hello, how can you help me?",
  "characterId": "character_123",
  "llmConfig": {
    "model": "claude-3-opus-20240229",
    "verbose": true
  },
  "chatHistory": [],
  "mcpServers": [
    {
      "name": "weather-service",
      "version": "1.0",
      "url": "https://example-mcp-server.com",
      "key": "optional_api_key"
    }
  ]
}

Close Chat

GET /api/chat/close

MCP Integration

The server connects to MCP servers specified in the request and retrieves available tools that can be used by the LangGraph agent.

Development

Run the development server with auto-reload:

npm run dev

Example Usage

// Send a message and stream the response
const response = await fetch("/api/chat", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    input: "What's the weather in New York?",
    characterId: "agent1",
    chatHistory: [],
    mcpServers: [
      {
        name: "weather-service",
        version: "1.0",
        url: "https://my-mcp-server.com",
      },
    ],
  }),
});

// Read the streaming response
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = new TextDecoder().decode(value);
const events = text.split("\n\n").filter(Boolean);
for (const event of events) {
const data = JSON.parse(event);
console.log(data);
}
}

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.