import mcp
from mcp.client.stdio import stdio_client, StdioServerParameters
#from mcp import StdioServerParameters
from agents import FunctionTool
import json


params = StdioServerParameters(
    command="python3",
    args=["smartfridge_server.py"],
)

async def list_groceries_tools():
    async with stdio_client(params) as streams:
        async with mcp.ClientSession(*streams) as session:
            await session.initialize()
            tools_result = await session.list_tools()
            return tools_result.tools
        
async def call_groceries_tool(tool_name, tool_args):
    async with stdio_client(params) as streams:
        async with mcp.ClientSession(*streams) as session:
            await session.initialize()
            result = await session.call_tool(tool_name, tool_args)
            return result
            
async def read_groceries_resource(name):
    async with stdio_client(params) as streams:
        async with mcp.ClientSession(*streams) as session:
            await session.initialize()
            result = await session.read_resource(f"groceries://smartfridge_server/{name}")
            return result.contents[0].text

async def get_groceries_tools_openai():
    openai_tools = []
    for tool in await list_groceries_tools():
        schema = {**tool.inputSchema, "additionalProperties": False}
        openai_tool = FunctionTool(
            name=tool.name,
            description=tool.description,
            params_json_schema=schema,
            on_invoke_tool=lambda ctx, args, toolname=tool.name: call_groceries_tool(toolname, json.loads(args))
                
        )
        openai_tools.append(openai_tool)
    return openai_tools