Skip to main content
Glama
digital-duck

Model Context Protocol Demo

by digital-duck
test_mcp_tool_resource.ipynb7.3 kB
{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "72c8db1b-ca44-4076-8395-475e2166d581", "metadata": {}, "outputs": [], "source": [ "from fastmcp import Client\n", "import asyncio\n", "import nest_asyncio\n", "\n", "# Allow nested event loops\n", "nest_asyncio.apply()" ] }, { "cell_type": "code", "execution_count": 2, "id": "4dafae42-876d-49f1-baf3-ad5ee5dc022f", "metadata": {}, "outputs": [], "source": [ "async def run_llm_demo(server_path: str = \"mcp_server.py\"):\n", " async with Client(server_path) as client:\n", " tools = await client.list_tools()\n", " print(f\"\\ntools:\\n {tools}\")\n", "\n", " resources = await client.list_resources()\n", " print(f\"\\nresources:\\n {resources}\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "2553885a-9df9-4bc5-bc0c-503559f6645d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "tools:\n", " [Tool(name='calculator', description='\\nPerforms arithmetic operations on two numbers.\\nSupports: add, subtract, multiply, divide, power\\n', inputSchema={'properties': {'operation': {'title': 'Operation', 'type': 'string'}, 'num1': {'title': 'Num1', 'type': 'number'}, 'num2': {'title': 'Num2', 'type': 'number'}}, 'required': ['operation', 'num1', 'num2'], 'type': 'object'}, annotations=None), Tool(name='trig', description='\\nPerforms trigonometric operations, with input/output angle (in unit of degree or radian).\\nSupports: sine, cosine, tangent, arc sine, arc cosine, arc tangent\\n', inputSchema={'properties': {'operation': {'title': 'Operation', 'type': 'string'}, 'num1': {'title': 'Num1', 'type': 'number'}, 'unit': {'title': 'Unit', 'type': 'string'}}, 'required': ['operation', 'num1', 'unit'], 'type': 'object'}, annotations=None), Tool(name='stock_quote', description='\\nRetrieves live stock data for a given ticker symbol.\\nExample: AAPL, GOOGL, MSFT, TSLA\\n', inputSchema={'properties': {'ticker': {'title': 'Ticker', 'type': 'string'}}, 'required': ['ticker'], 'type': 'object'}, annotations=None), Tool(name='health', description='\\nSimple health check to verify server is running.\\n', inputSchema={'properties': {}, 'type': 'object'}, annotations=None), Tool(name='echo', description='\\nEcho back the provided message. Useful for testing.\\n', inputSchema={'properties': {'message': {'title': 'Message', 'type': 'string'}}, 'required': ['message'], 'type': 'object'}, annotations=None)]\n", "\n", "resources:\n", " [Resource(uri=AnyUrl('info://server'), name='info://server', description=None, mimeType='text/plain', size=None, annotations=None)]\n" ] } ], "source": [ "# Now asyncio.run() will work in Jupyter\n", "asyncio.run(run_llm_demo())" ] }, { "cell_type": "code", "execution_count": 4, "id": "5f746e72-1539-46b5-82ae-3f50ac5c044f", "metadata": {}, "outputs": [], "source": [ "async def debug_resources():\n", " async with Client(\"mcp_server.py\") as client:\n", " print(\"=== Tools ===\")\n", " tools = await client.list_tools()\n", " for tool in tools:\n", " print(f\"Tool: {tool.name} - {tool.description}\")\n", " \n", " print(\"\\n=== Resources ===\")\n", " resources = await client.list_resources()\n", " for resource in resources:\n", " print(f\"Resource: {resource.uri} - {resource.description}\")\n", " \n", " print(\"\\n=== Test Reading Specific Stock Resource ===\")\n", " try:\n", " # Test reading a specific ticker resource\n", " result = await client.read_resource(\"stock://AAPL\")\n", " print(\"✅ Successfully read stock://AAPL\")\n", " if hasattr(result, 'content') and result.content:\n", " print(f\"Content: {result.content[0].text}\")\n", " elif isinstance(result, list) and result:\n", " print(f\"Content: {result[0].text if hasattr(result[0], 'text') else result[0]}\")\n", " else:\n", " print(f\"Raw result: {result}\")\n", " except Exception as e:\n", " print(f\"❌ Error reading stock://AAPL: {e}\")\n", " \n", " print(\"\\n=== Test Reading Server Info Resource ===\")\n", " try:\n", " result = await client.read_resource(\"info://server\")\n", " print(\"✅ Successfully read info://server\")\n", " if hasattr(result, 'content') and result.content:\n", " print(f\"Content: {result.content[0].text}\")\n", " elif isinstance(result, list) and result:\n", " print(f\"Content: {result[0].text if hasattr(result[0], 'text') else result[0]}\")\n", " else:\n", " print(f\"Raw result: {result}\")\n", " except Exception as e:\n", " print(f\"❌ Error reading info://server: {e}\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "3ea01af3-396b-465e-8ea3-c2ac93d7ca52", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== Tools ===\n", "Tool: calculator - \n", "Performs arithmetic operations on two numbers.\n", "Supports: add, subtract, multiply, divide, power\n", "\n", "Tool: trig - \n", "Performs trigonometric operation on an angle in degree.\n", "Supports: sine, cosine, tangent\n", "\n", "Tool: stock_quote - \n", "Retrieves live stock data for a given ticker symbol.\n", "Example: AAPL, GOOGL, MSFT, TSLA\n", "\n", "Tool: health - \n", "Simple health check to verify server is running.\n", "\n", "Tool: echo - \n", "Echo back the provided message. Useful for testing.\n", "\n", "\n", "=== Resources ===\n", "Resource: info://server - None\n", "\n", "=== Test Reading Specific Stock Resource ===\n", "✅ Successfully read stock://AAPL\n", "Content: Apple Inc. (AAPL) - A publicly traded company\n", "\n", "=== Test Reading Server Info Resource ===\n", "✅ Successfully read info://server\n", "Content: This is a demo MCP server built with FastMCP. It provides calculator and stock quote tools.\n" ] } ], "source": [ "await debug_resources()" ] }, { "cell_type": "code", "execution_count": null, "id": "4c20a037-b471-412d-8869-182140ed5c21", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 5 }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/digital-duck/mcp_demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server