Mcp client example

by t-shiratori

257 downloads
Not rated
GitHub

About

This is a sample CLI chat application for an AI assistant using the Anthropic API in conjunction with an MCP client and MCP server.

Details

Author
t-shiratori
Downloads
257
Categories
AI

- CLI chat interface using Anthropic API.
- Connects to an MCP server over stdio transport.
- Automatically calls MCP tools when the AI requires them.
- Includes a sequence diagram for the full interaction flow.
- References a public time‑tools MCP server as an example.

Set up by running pnpm install, then configure a .env file with ANTHROPIC_API_KEY and MCP_SERVER_SCRIPT_PATH. Build with pnpm build and run with pnpm start. The app connects to an MCP server via stdio and enters a chat loop until the user types ‘quit’.

Mcp client example

This is a sample CLI chat application for an AI assistant using the Anthropic API in conjunction with an MCP client and MCP server.

Reference:
For Client Developers - Model Context Protocol

Anthropic API:
Initial setup - Anthropic
Messages - Anthropic

<b>System diagram</b>

<b>Sequence diagram</b>

sequenceDiagram
    participant User
    participant MCPClient
    participant AnthropicAPI
    participant MCPServer

User->>MCPClient: Run
MCPClient->>MCPServer: Connect and get tool info
MCPServer-->>MCPClient: Tool info

loop Chat loop (until 'quit' is entered)
User->>MCPClient: Enter query
MCPClient->>AnthropicAPI: Send query
AnthropicAPI-->>MCPClient: AI response

alt When tool usage is required
MCPClient->>MCPServer: Call tool
MCPServer-->>MCPClient: Tool execution result
MCPClient->>AnthropicAPI: Send query again including tool result
AnthropicAPI-->>MCPClient: Final response
end

MCPClient-->>User: Display response
end

User->>MCPClient: Enter 'quit'
MCPClient->>MCPServer: Disconnect
MCPClient-->>User: Exit

Setup

pnpm install

.env

ANTHROPIC_API_KEY=your-api-key
MCP_SERVER_SCRIPT_PATH=/path/to/mcp-server.js

Build

pnpm build

Run

pnpm start

Output example

Use this mcp server
https://github.com/t-shiratori/time-tools-mcp-server

``shell-session

mcp-client-example $ pnpm start

> mcp-client-example@1.0.0 start /path/to/mcp-client-example
> node build/index.js

Example MCP Server running on stdio
Connected to server with tools: [ 'get_current_date_time', 'get_elapsed_time' ]

MCP Client Started!
Type your queries or 'quit' to exit.

🖌 Query: Hello

Hello! I'm here to assist you. I can help you with tasks related to date and time, such as getting the current date and time or calculating the elapsed time between two dates.

Is there something specific you'd like to know about dates or times today?

🖌 Query: What time is it?

I can check the current date and time for you.
[Calling tool get_current_date_time with args {}]
Based on the given timestamp, the time is 10:37:19 AM on May 6th, 2025.

``

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.