Skip to main content
Glama

unichat

Chat with an AI assistant to review documents, evaluate proposals, or answer questions by providing a system message and user query.

Instructions

Chat with an assistant. Example tool use message: Ask the unichat to review and evaluate your proposal.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
messagesYesArray of exactly two messages: first a system message defining the task, then a user message with the specific query

Implementation Reference

  • The handler function for the 'unichat' tool. It validates the tool name, checks the messages input, calls the UnifiedChatApi to generate a response using the specified model, formats the response, and returns it as TextContent.
    @server.call_tool()
    async def handle_call_tool(name: str, arguments: dict | None) -> list[types.TextContent]:
        if name != "unichat":
            logger.error(f"Unknown tool requested: {name}")
            raise ValueError(f"Unknown tool: {name}")
    
        try:
            logger.debug("Validating messages")
            validate_messages(arguments.get("messages", []))
    
            response = chat_api.chat.completions.create(
                model=MODEL,
                messages=arguments["messages"],
                stream=False
            )
    
            response = format_response(response.choices[0].message.content)
    
            return [response]
        except Exception as e:
            logger.error(f"Error calling tool: {str(e)}")
            raise Exception(f"An error occurred: {e}")
  • The JSON schema defining the input structure for the 'unichat' tool, requiring an object with a 'messages' array of exactly two items (system and user roles).
    inputSchema={
        "type": "object",
        "properties": {
            "messages": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "role": {
                            "type": "string",
                            "description": "The role of the message sender. Must be either 'system' or 'user'",
                            "enum": ["system", "user"]
                        },
                        "content": {
                            "type": "string",
                            "description": "The content of the message. For system messages, this should define the context or task. For user messages, this should contain the specific query."
                        },
                    },
                    "required": ["role", "content"],
                },
                "minItems": 2,
                "maxItems": 2,
                "description": "Array of exactly two messages: first a system message defining the task, then a user message with the specific query"
            },
        },
        "required": ["messages"],
    },
  • The list_tools handler that registers and exposes the 'unichat' tool, including its name, description, and input schema.
    @server.list_tools()
    async def handle_list_tools() -> list[types.Tool]:
        return [
            types.Tool(
                name="unichat",
                description="""Chat with an assistant.
                            Example tool use message:
                            Ask the unichat to review and evaluate your proposal.
                            """,
                inputSchema={
                    "type": "object",
                    "properties": {
                        "messages": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "role": {
                                        "type": "string",
                                        "description": "The role of the message sender. Must be either 'system' or 'user'",
                                        "enum": ["system", "user"]
                                    },
                                    "content": {
                                        "type": "string",
                                        "description": "The content of the message. For system messages, this should define the context or task. For user messages, this should contain the specific query."
                                    },
                                },
                                "required": ["role", "content"],
                            },
                            "minItems": 2,
                            "maxItems": 2,
                            "description": "Array of exactly two messages: first a system message defining the task, then a user message with the specific query"
                        },
                    },
                    "required": ["messages"],
                },
            ),
        ]
  • Helper function to validate the messages input for the unichat tool, ensuring exactly two messages with correct roles.
    def validate_messages(messages):
        logger.debug(f"Validating messages: {len(messages)} messages received")
        if len(messages) != 2:
            logger.error(f"Invalid number of messages: {len(messages)}")
            raise ValueError("Exactly two messages are required: one system message and one user message")
    
        if messages[0]["role"] != "system":
            logger.error("First message has incorrect role")
            raise ValueError("First message must have role 'system'")
    
        if messages[1]["role"] != "user":
            logger.error("Second message has incorrect role")
            raise ValueError("Second message must have role 'user'")
  • Helper function to format the chat API response into the required TextContent type.
    def format_response(response: str) -> types.TextContent:
        logger.debug("Formatting response")
        try:
            formatted = {"type": "text", "text": response.strip()}
            logger.debug("Response formatted successfully")
            return formatted
        except Exception as e:
            logger.error(f"Error formatting response: {str(e)}")
            return {"type": "text", "text": f"Error formatting response: {str(e)}"}
Install Server

Other Tools

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/amidabuddha/unichat-mcp-server'

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