forked from dremio/dremio-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_tools.py
More file actions
95 lines (76 loc) · 3.26 KB
/
ai_tools.py
File metadata and controls
95 lines (76 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#
# Copyright (C) 2017-2025 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pydantic import BaseModel, Field, ConfigDict
from typing import Dict, List, Optional, Any
from urllib.parse import quote
from aiohttp import ClientResponseError
from dremioai.api.transport import DremioAsyncHttpClient as AsyncHttpClient
from dremioai.config import settings
from dremioai.log import logger
log = logger(__name__)
class AiTool(BaseModel):
name: str
description: Optional[str] = None
input_schema: Dict[str, Any] = Field(
default_factory=lambda: {"type": "object"}, alias="inputSchema"
)
model_config = ConfigDict(extra="allow", populate_by_name=True)
class ListToolsResponse(BaseModel):
tools: List[AiTool] = Field(default_factory=list)
error: Optional[str] = None
def __bool__(self):
return self.error is None
class InvokeToolResponse(BaseModel):
result: Optional[Any] = None
error: Optional[str] = None
def __bool__(self):
return self.error is None
@property
def is_empty(self) -> bool:
"""True when the response carries neither a result nor an error.
This can happen when Dremio returns a 200 with an empty body for a
void tool. Callers may choose to treat this as a successful no-op.
"""
return self.result is None and self.error is None
async def list_tools() -> ListToolsResponse:
try:
client = AsyncHttpClient()
project_id = settings.instance().dremio.project_id
endpoint = f"/v1/projects/{project_id}" if project_id else "/api/v4"
return await client.get(f"{endpoint}/ai/tools", deser=ListToolsResponse)
except ClientResponseError as e:
log.exception("Failed to list AI tools")
return ListToolsResponse(error=f"HTTP {e.status} {e.message}")
except Exception:
log.exception("Failed to list AI tools")
return ListToolsResponse(error="Unexpected error listing AI tools")
async def invoke_tool(tool_name: str, args: Dict[str, Any]) -> InvokeToolResponse:
safe_name = quote(tool_name, safe="")
try:
client = AsyncHttpClient()
project_id = settings.instance().dremio.project_id
endpoint = f"/v1/projects/{project_id}" if project_id else "/api/v4"
return await client.post(
f"{endpoint}/ai/tools/{safe_name}:invoke",
body={"args": args},
deser=InvokeToolResponse,
)
except ClientResponseError as e:
log.exception("Failed to invoke AI tool '%s'", tool_name)
return InvokeToolResponse(error=f"HTTP {e.status} {e.message}")
except Exception:
log.exception("Failed to invoke AI tool '%s'", tool_name)
return InvokeToolResponse(error=f"Unexpected error invoking tool '{tool_name}'")