mcp_demonstration

Pvbhat123/mcp_demonstration

3.1

If you are the rightful owner of mcp_demonstration and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

This document provides a comprehensive overview of a Model Context Protocol (MCP) server implementation using JSON-RPC 2.0.

Tools
2
Resources
0
Prompts
0

Standard MCP Implementation with JSON-RPC

This is a standard-compliant Model Context Protocol (MCP) implementation using JSON-RPC 2.0 protocol.

Architecture

1. MCP Server (Port 4001)

  • Implements MongoDB operations as MCP tools
  • Uses JSON-RPC 2.0 protocol
  • Supports standard MCP methods: initialize, tools/list, tools/call
  • Proper error handling with standard error codes

2. MCP Client (Port 4002)

  • Acts as orchestrator between Host and Server
  • Maintains tool registry from multiple servers
  • Routes tool calls using JSON-RPC protocol
  • Supports server initialization handshake

3. MCP Host (Interactive CLI)

  • User interface with Gemini AI integration
  • Communicates via JSON-RPC with the Client
  • Processes natural language queries
  • Formats results for user consumption

Key Differences from REST Implementation

  1. Protocol: Uses JSON-RPC 2.0 instead of REST
  2. Message Format: All requests/responses follow JSON-RPC envelope:
    {
      "jsonrpc": "2.0",
      "method": "method_name",
      "params": {},
      "id": "unique_id"
    }
    
  3. Error Handling: Standard JSON-RPC error codes
  4. Single Endpoint: All communication through /rpc endpoint
  5. Request Correlation: Uses request IDs for tracking
  6. Initialization: Proper protocol handshake between components

Setup Instructions

Prerequisites

  • Node.js installed
  • MongoDB running on localhost:27017
  • Gemini API key

Installation

  1. Install dependencies for each component:

    cd mcp_standard/mcp_server
    npm install
    
    cd ../mcp_client
    npm install
    
    cd ../mcp_host
    npm install
    
  2. Set up environment variables: Create a .env file in mcp_standard/mcp_host/:

    GEMINI_API_KEY=your-gemini-api-key
    

Running the System

Start each component in separate terminals:

  1. Start MCP Server:

    cd mcp_standard/mcp_server
    npm start
    
  2. Start MCP Client:

    cd mcp_standard/mcp_client
    npm start
    
  3. Start MCP Host (Interactive CLI):

    cd mcp_standard/mcp_host
    npm start
    

Testing the Implementation

Once all components are running, you can interact with the system through the Host CLI:

👤 Your question: list all databases
🤖 Processing...

📋 Response:
============================================================
I found 3 databases in your MongoDB instance:

1. **admin** - System database (320 KB)
2. **config** - Configuration database (192 KB)  
3. **local** - Local database (256 KB)

All databases are currently active and not empty.
============================================================

Verifying MCP Compliance

This implementation follows the MCP standard by:

  1. Using JSON-RPC 2.0 protocol for all communications
  2. Implementing required methods: initialize, tools/list, tools/call
  3. Following standard error codes (-32700 to -32603)
  4. Supporting protocol version negotiation
  5. Using proper request/response correlation with IDs
  6. Implementing tool discovery with JSON Schema for parameters

API Examples

Initialize Request

{
  "jsonrpc": "2.0",
  "method": "initialize",
  "params": {
    "protocolVersion": "1.0",
    "clientInfo": {
      "name": "mcp-client",
      "version": "1.0.0"
    }
  },
  "id": "init_1"
}

Tool Call Request

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "list_collections",
    "arguments": {
      "database_name": "mydb"
    }
  },
  "id": "call_1"
}

Monitoring and Logging

JSON-RPC Message Logs

The implementation includes comprehensive JSON-RPC message logging. Each component logs all requests and responses in a formatted manner:

Log Files:

  • logs/mcp-server-jsonrpc.log - Server incoming/outgoing messages
  • logs/mcp-client-jsonrpc.log - Client incoming messages from host
  • logs/mcp-client-outgoing-jsonrpc.log - Client outgoing messages to server
  • logs/mcp-host-jsonrpc.log - Host outgoing messages to client

Log Format Example:

============================================================
Timestamp: 2024-01-20T10:30:45.123Z
Component: mcp-server
Direction: INCOMING
Type: REQUEST
============================================================
{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "params": {},
  "id": "req_1234567890_1"
}
============================================================

============================================================
Timestamp: 2024-01-20T10:30:45.125Z
Component: mcp-server
Direction: OUTGOING
Type: RESPONSE
============================================================
{
  "jsonrpc": "2.0",
  "result": {
    "tools": [
      {
        "name": "list_databases",
        "description": "List all databases in MongoDB instance",
        "inputSchema": {
          "type": "object",
          "properties": {},
          "required": []
        }
      }
    ]
  },
  "id": "req_1234567890_1"
}
============================================================

Viewing Logs

Use the included log viewer for real-time monitoring:

cd mcp_standard
node view-logs.js

This provides:

  • Real-time log tailing
  • Color-coded output
  • Combined view of all components
  • Log clearing functionality

Standard Log Files

Each component also maintains standard operation logs:

  • mcp-server-standard.log
  • mcp-client-standard.log
  • mcp-host-standard.log