azure function mcp server and client codes

 The following code allows us to setup a server and then expose a tool called hello_mcp. Ensure you start azurite and then run "func host start" to run the server


import azure.functions as func
import logging

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.mcp_tool_trigger(
    arg_name="context",
    type="mcpToolTrigger",
    tool_name="hello_mcp",
    description="Hello world.",
    toolProperties="[]",
)
def mcp_trigger(context) -> None:
    return "Hello I am MCPTool!"

And during runtime, we will see the following endpoint 




Then we can also call it using the following client side code where we are caling it using streamable http.



"""
Simple MCP client that connects to a local Azure Functions MCP endpoint
(the mcpToolTrigger extension approach) and calls the `hello_mcp` tool.

Install dependency first:
    pip install mcp

Make sure your Function app is running locally first:
    func start
"""

import asyncio

from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client

MCP_URL = "http://localhost:7071/runtime/webhooks/mcp"


async def main():
    async with streamable_http_client(MCP_URL) as (read_stream, write_stream, _):
        async with ClientSession(read_stream, write_stream) as session:
            # Required handshake before doing anything else
            await session.initialize()

            # List available tools (should include "hello_mcp")
            tools_result = await session.list_tools()
            print("Available tools:")
            for tool in tools_result.tools:
                print(f"  - {tool.name}: {tool.description}")
            print()

            # Call the hello_mcp tool
            result = await session.call_tool("hello_mcp", arguments={})

            print("Tool result:")
            for content in result.content:
                if content.type == "text":
                    print(content.text)


if __name__ == "__main__":
    asyncio.run(main())





Comments

Popular posts from this blog

gemini cli getting file not defined error

mongosh install properly

llama cpp running it in google colab