Sponsored by Deepsite.site

MCPClient Python Application

Created By
spirita12049 months ago
implementation for interacting between an MCP server and an Ollama model
Content

MCPClient Python Application

This is a Python client application designed to interact with an MCP (Model Context Protocol) server.

Features

  • Asynchronous Communication: Uses asyncio for non-blocking communication between the client and server.
  • Customizable Server Scripts: The client can connect to both Python and JavaScript-based server scripts.
  • Tool Management: Dynamically fetches and interacts with tools available on the connected server.
  • Chat Interface: Provides a simple command-line interface to interact with the server in a conversational format.
  • Tool Integration: Supports extracting JSON-formatted tool calls from server responses and executing them.
  • Environment Variable Loading: Supports loading environment variables from a .env file using the dotenv package.

Requirements

  • Python 3.7 or higher
  • asyncio library (included with Python)
  • requests for HTTP requests to the server
  • mcp (custom library for handling MCP communication)
  • dotenv for environment variable management

Setup

  1. Clone the repository (or download the script files) to your local machine.

  2. Install required dependencies:

    pip install -r requirements.txt
    
  3. Create a .env file in the root directory to load necessary environment variables. For example:

    BASE_URL=http://localhost:11434
    MODEL=llama3.2
    
  4. Run the client with the path to the server script:

    python client.py <server_script_path>
    

    The server script can be a Python .py or JavaScript .js file.

How It Works

  1. Connecting to the MCP Server: The client connects to the server via standard input/output channels, using the provided script (.py or .js).
  2. Processing Queries: The client sends user queries to the server and receives responses. Available tools are listed and can be called directly from the assistant’s replies.
  3. Tool Execution: If a response contains a valid tool call (in JSON format), the client extracts the call and triggers the respective tool on the server.
  4. Interaction: The client interacts with the server in a conversational format, displaying results from server tools and continuing the conversation.

Example Workflow

  1. The user enters a query like:

    Question: What is the weather today?
    
  2. The client sends the query to the server, which responds with available tools and information.

  3. If the server suggests using a weather tool, the client executes the tool with the necessary parameters and shows the result.

  4. The client continues the conversation based on the new information returned by the tool. https://github.com/furey/mongodb-lens

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
CursorThe AI Code Editor
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Tavily Mcp
WindsurfThe new purpose-built IDE to harness magic
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
DeepChatYour AI Partner on Desktop
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Playwright McpPlaywright MCP server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
ChatWiseThe second fastest AI chatbot™
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Serper MCP ServerA Serper MCP Server
Amap Maps高德地图官方 MCP Server