Guide
A repository containing the basic MCP client and MCP server (based on SDK and JSON-RPC implementation) to help everyone learn MCP from 0
Installation
npx mcp-guideAsk AI about Guide
Powered by Claude · Grounded in docs
I know everything about Guide. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
MCP Guide
A repository containing the basic MCP client and MCP server (based on SDK and JSON-RPC implementation) to help everyone learn MCP from 0.
Project Structure
mcp-client.ts: A command-line interface (CLI) client that connects to MCP servers and uses OpenAI's API to process user queries and interact with the available tools.mcp-server-v1.ts: An MCP server implementation using the@modelcontextprotocol/sdklibrary. It provides weather tools (get-alertsandget-forecast) over stdio.mcp-server-v2.ts: Another MCP server implementation, similar tov1, but using a slightly different server setup from the SDK. It also provides weather tools over stdio.mcp-server-protocol.ts: An MCP server implemented as an HTTP server using Express.js. It manually implements the JSON-RPC based MCP, providing the same weather tools over an HTTP endpoint.utils.ts: Contains utility functions for interacting with the NWS API, including making requests and formatting alert data.types.ts: Defines TypeScript types and Zod schemas for validating arguments and structuring data related to the NWS API.
Features
- Multiple Server Implementations:
- Stdio-based MCP server (v1 and v2 using the SDK).
- HTTP-based MCP server (manual protocol implementation).
- Weather Tools:
get-alerts: Fetches weather alerts for a given US state.get-forecast: Retrieves the weather forecast for a specific latitude and longitude.
- OpenAI Integration: The client uses an OpenAI model to understand natural language queries and decide which tool to call.
- Dynamic Server Connection: The client can connect to multiple configured MCP servers.
Setup
- Install Dependencies:
npm install - Environment Variables:
Create a
.envfile in the root of the project and add your OpenAI API key:OPENAI_API_KEY=your_openai_api_key_here # Optional: Specify a different OpenAI model or base URL # OPENAI_MODEL=gpt-3.5-turbo # OPENAI_BASE_URL=https://api.openai.com/v1
Running the Servers
You can run the different server implementations using the following commands:
- MCP Server V1 (stdio):
tsx mcp-server-v1.ts - MCP Server V2 (stdio):
tsx mcp-server-v2.ts - MCP Server (HTTP):
This server will typically run ontsx mcp-server-protocol.tshttp://localhost:3000.
Running the Client
The client is configured in mcp-client.ts. By default, it attempts to connect to servers marked with isOpen: true in its configuration.
To run the client:
tsx mcp-client.ts
Once connected, you can type your weather-related queries, for example:
- "What are the weather alerts for CA?"
- "Get the forecast for latitude 34.05 and longitude -118.24"
The client will interact with the connected MCP server(s) and OpenAI to provide the requested information. Type quit to exit the client.
How it Works
- The
mcp-client.tsstarts and connects to the configured MCP servers. - It lists the available tools from each connected server.
- When the user enters a query, the client sends this query along with the list of available tools to an OpenAI model.
- The OpenAI model determines if any tool can fulfill the request.
- If a tool is chosen, the client calls the respective tool on the appropriate MCP server with the arguments identified by the OpenAI model.
- The MCP server executes the tool (e.g., by calling the NWS API) and returns the result.
- The client receives the tool's result and sends it back to the OpenAI model along with the original query and conversation history.
- The OpenAI model generates a final response for the user based on the tool's output.
- The client displays this response.
Notes
- The NWS API primarily provides data for locations within the United States.
- The
mcp-server-v1.tsandmcp-server-v2.tsuse the@modelcontextprotocol/sdkfor easier MCP implementation over stdio. - The
mcp-server-protocol.tsdemonstrates a manual implementation of the MCP over HTTP, which involves handling JSON-RPC messages directly. - Error handling is implemented in both client and server components.
- The client can be extended to support more servers or different types of transport (e.g., SSE).
