Thursday, July 30, 2026

Python script connect to local MCP(Model Context Protocol) Server and list available tools


MCP (Model Context Protocol) is an open standard that acts like "USB-C for AI applications." What is MCP?Introduced by Anthropic in late 2024 and now governed as an open, vendor-neutral protocol, MCP provides a standardized way for AI models and applications (like Claude, ChatGPT, or custom agents) to securely connect to external data sources, tools, and workflows. Instead of building custom integrations for every tool or database, developers create lightweight MCP servers that expose:
  • Resources (files, documents, database records, etc.)
  • Tools (actions like querying APIs, updating systems, or running code)
  • Prompts (reusable workflows or instructions)
AI applications use MCP clients to discover and interact with these servers via a consistent protocol (based on JSON-RPC).How Does MCP Help?
  • For users: AI becomes far more capable and personalized — it can access your files, calendars, databases, or business tools in real time and take meaningful actions, rather than just generating text.
  • For developers: It dramatically reduces integration complexity. One standard replaces dozens of bespoke connections, speeding up development while improving security and maintainability.
  • For the ecosystem: It enables plug-and-play interoperability across different AI models, tools, and platforms.
In short, MCP turns isolated AI chatbots into powerful, context-aware agents that can work with the real-world systems that matter. It's a foundational technology for building the next generation of practical AI applications.



Command Prompt Command to create Local MCP Server

C:\>npx @modelcontextprotocol/server-filesystem C:\Users\Ee" "Leen\mcp_test


Python script to act as MCP client to call the MCP server

Execute python script command: python mcp_filesystem_test.py

Python Script by ChatGPT

import asyncio

from mcp import ClientSession, StdioServerParameters

from mcp.client.stdio import stdio_client


async def main():

    server = StdioServerParameters(

        command="npx",

        args=[

            "@modelcontextprotocol/server-filesystem",

            r"C:\Users\Ee Leen\mcp_test"

        ]

    )


    async with stdio_client(server) as (read, write):

        async with ClientSession(read, write) as session:

            await session.initialize()

            tools = await session.list_tools()

            print("Available MCP tools:")

            for tool in tools.tools:

                print("-", tool.name)

asyncio.run(main())



No comments:

Post a Comment