Skip to content

STREAMLINE EXAMPLES #19

@tendaworld

Description

@tendaworld

To enhance the usability and clarity of the mcp-client-cli project, I propose updating the README.md file to include a comprehensive example demonstrating how to set up and run an MCP server and connect it to the MCP CLI client. This addition will provide users with a practical guide, facilitating a smoother integration process.

Proposed Enhancements:

  1. Detailed MCP Server Example:

    • Objective: Provide a step-by-step guide to creating a basic MCP server in Python, illustrating the implementation of tools and resources.
    • Content:
      • Server Initialization: Instructions on setting up the server using FastMCP.
      • Tool Definition: Examples such as add (addition) and multiply (multiplication) functions.
      • Resource Definition: An example like get_greeting to return personalized messages.
      • Asynchronous Tool Example: An asynchronous function like get_weather to demonstrate handling of async operations.
      • Example MCP server in Python:
        from mcp.server.fastmcp import FastMCP
        
        # Initialize an MCP server instance with a name
        mcp = FastMCP("Demo Server")
        
        # Define a tool function (simple addition)
        @mcp.tool()
        def add(a: int, b: int) -> int:
            """Add two numbers."""
            return a + b
        
        # Define another tool function (multiplication)
        @mcp.tool()
        def multiply(a: int, b: int) -> int:
            """Multiply two numbers."""
            return a * b
        
        # Define an asynchronous tool function (fetch weather)
        @mcp.tool()
        async def get_weather(location: str) -> str:
            """Get weather for a location."""
            return f"It's always sunny in {location}"
        
        # Define a resource function (personalized greeting)
        @mcp.resource("greeting://{name}")
        def get_greeting(name: str) -> str:
            """Get a greeting message for the given name."""
            return f"Hello, {name}!"
        
        # Run the MCP server
        if __name__ == "__main__":
            mcp.run()
  2. Server Execution Instructions:

    • Direct Execution: Guidelines on running the server script directly using python server.py.
    • Using MCP CLI: Instructions on utilizing the MCP CLI's shortcut command mcp run server.py to start the server.
  3. MCP CLI Client Configuration:

    • Objective: Guide users on configuring the MCP CLI client to connect with the newly created server.
    • Content:
      • Configuration File Setup: Detailed instructions on creating and modifying the ~/.llm/config.json file to specify LLM providers and MCP server commands.
      • Example Configuration:
        {
          "systemPrompt": "You are an AI assistant helping a software engineer...",
          "llm": {
            "provider": "openai",
            "model": "gpt-4",
            "api_key": "your-openai-api-key",
            "temperature": 0.7,
            "base_url": "https://api.openai.com/v1"
          },
          "mcpServers": {
            "demo-server": {
              "command": "python",
              "args": ["server.py"],
              "requires_confirmation": ["add", "multiply", "get_weather"]
            }
          }
        }
      • Explanation: Clarify each configuration parameter, such as command, args, and requires_confirmation, to ensure users understand how to customize the setup for their specific needs.

Benefits of the Update:

  • Improved Accessibility: Users, especially those new to MCP, will have a clear, practical example to follow, reducing the learning curve.
  • Enhanced Integration: A detailed guide will facilitate seamless integration between the MCP server and the MCP CLI client, promoting effective utilization of the tool.
  • Increased Adoption: Comprehensive documentation can attract more users to adopt the tool, knowing that they have clear guidance on setup and usage.

Current Results:

  • MCP Server Integration fails.

^Can error occurred during closing of asynchronous generator <async_generator object stdio_client at 0x7ff5367ce640>
asyncgen: <async_generator object stdio_client at 0x7ff5367ce640> Traceback (most recent call last):
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp/client/stdio.py", line 153, in stdio_client
yield read_stream, write_stream
GeneratorExit

During handling of the above exception, another exception occurred:

+ Exception Group Traceback (most recent call last):
| File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 767, in __aexit__
| raisecBaseExceptionGroup(
| BaseExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp/client/stdio.py", line 153, in stdio_client
| yield read_stream, write_stream
| GeneratorExit
+------------------------------------

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp/client/stdio.py", line 148, in stdio_client
anyio.create_task_group() as tg,
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 773, in __aexit__
if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 456, in __exit__
raise RuntimeError(
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
Traceback (most recent call last):
File "/home/lamnot/.pyenv/versions/3.12.9/lib/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lamnot/.pyenv/versions/3.12.9/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp_client_cli/cli.py", line 58, in run
await handle_list_tools(app_config, args)
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp_client_cli/cli.py", line 126, in handle_list_tools
toolkits, tools = await load_tools(server_configs, args.no_tools, args.force_refresh)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp_client_cli/cli.py", line 179, in load_tools
async with anyio.create_task_group() as tg:
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 771, in __aexit__
raise exc_val
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 743, in __aexit__
await self._on_completed_fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/lamnot/ai/mcp/env/bin/llm", line 8, in
sys.exit(main())
^^^^^^
File "/home/lamnot/ai/mcp/env/lib/python3.12/site-packages/mcp_client_cli/cli.py", line 358, in main
asyncio.run(run())
File "/home/lamnot/.pyenv/versions/3.12.9/lib/python3.12/asyncio/runners.py", line 195, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/home/lamnot/.pyenv/versions/3.12.9/lib/python3.12/asyncio/runners.py", line 123, in run
raise KeyboardInterrupt()
KeyboardInterrupt

Conclusion:

By incorporating these enhancements into the README.md, we can significantly improve the user experience, making the mcp-client-cli more approachable and easier to integrate into various workflows.

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions