-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmcp.py
More file actions
442 lines (381 loc) · 13.8 KB
/
mcp.py
File metadata and controls
442 lines (381 loc) · 13.8 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#
# 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 mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.prompts import Prompt
from mcp.server.fastmcp.resources import FunctionResource
from mcp.cli.claude import get_claude_config_path
from pydantic.networks import AnyUrl
from dremioai.tools import tools
import os
from typing import List, Union, Annotated, Optional, Tuple, Dict, Any
from functools import reduce
from operator import ior
from pathlib import Path
from dremioai import log
from typer import Typer, Option, Argument, BadParameter
from rich import console, table, print as pp
from click import Choice
from dremioai.config import settings
from dremioai.api.oauth2 import get_oauth2_tokens
from enum import StrEnum, auto
from json import load, dump as jdump
from shutil import which
import asyncio
from yaml import dump, add_representer
import sys
def init(
uri: str = None,
pat: str = None,
project_id: str = None,
mode: Union[tools.ToolType, List[tools.ToolType]] = None,
) -> FastMCP:
mcp = FastMCP("Dremio", level="DEBUG")
mode = reduce(ior, mode) if mode is not None else None
for tool in tools.get_tools(For=mode):
tool_instance = tool()
mcp.add_tool(
tool_instance.invoke,
name=tool.__name__,
description=tool_instance.invoke.__doc__,
)
for resource in tools.get_resources(For=mode):
resource_instance = resource()
mcp.add_resource(
FunctionResource(
uri=AnyUrl(resource_instance.resource_path),
name=resource.__name__,
description=resource.__doc__,
mime_type="application/json",
fn=resource_instance.invoke,
)
)
# if mode is None or (mode & tools.ToolType.FOR_SELF) != 0:
mcp.add_prompt(
Prompt.from_function(tools.system_prompt, "System Prompt", "System Prompt")
)
return mcp
app = None
# if __name__ != "__main__":
# if mode := os.environ.get("MODE"):
# mode = [tools.ToolType[m.upper()] for m in ",".split(mode)]
# app = init(mode=mode)
def _mode() -> List[str]:
return [tt.name for tt in tools.ToolType]
ty = Typer(context_settings=dict(help_option_names=["-h", "--help"]))
@ty.command(name="run", help="Run the DremioAI MCP server")
def main(
dremio_uri: Annotated[Optional[str], Option(help="Dremio URI")] = None,
dremio_pat: Annotated[Optional[str], Option(help="Dremio PAT")] = None,
dremio_username: Annotated[Optional[str], Option(help="Dremio username")] = None,
dremio_password: Annotated[Optional[str], Option(help="Dremio password")] = None,
dremio_project_id: Annotated[
Optional[str], Option(help="Dremio Project Id")
] = None,
config_file: Annotated[
Optional[Path],
Option("-c", "--cfg", help="The config yaml for various options"),
] = None,
mode: Annotated[
Optional[List[str]],
Option("-m", "--mode", help="MCP server mode", click_type=Choice(_mode())),
] = None,
list_tools: Annotated[
bool, Option(help="List available tools for this mode and exit")
] = False,
log_to_file: Annotated[Optional[bool], Option(help="Log to file")] = False,
):
if not list_tools:
log.configure(enable_json_logging=True, to_file=True)
else:
log.configure(enable_json_logging=True, to_file=log_to_file)
log.set_level("DEBUG")
if mode is not None:
mode = [tools.ToolType[m.upper()] for m in mode]
cfg = (
settings.configure(config_file)
.get()
.with_overrides(
{
"dremio.uri": dremio_uri,
"dremio.pat": dremio_pat,
"dremio.username": dremio_username,
"dremio.password": dremio_password,
"dremio.project_id": dremio_project_id,
"tools.server_mode": mode,
}
)
)
if list_tools:
log.logger().info(f"Starting Dremio tools with {cfg}")
mode = reduce(ior, mode) if mode is not None else None
log.logger().info(f"Listing available tools for mode={mode}")
for tool in tools.get_tools(For=mode):
print(tool.__name__)
return
dremio = settings.instance().dremio
if (
dremio.oauth_supported
and dremio.oauth_configured
and (dremio.oauth2.has_expired or dremio.pat is None)
):
oauth = get_oauth2_tokens()
oauth.update_settings()
app = init(
uri=cfg.dremio.uri,
pat=cfg.dremio.pat,
project_id=cfg.dremio.project_id,
mode=cfg.tools.server_mode,
)
app.run()
tc = Typer(
context_settings=dict(help_option_names=["-h", "--help"]),
name="config",
help="Configuration management",
)
class ConfigTypes(StrEnum):
dremioai = auto()
claude = auto()
def get_claude_config_path() -> Path:
# copy of the function from mcp sdk, but returns the path whether or not
# it exists
dir = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config"), "Claude")
match sys.platform:
case "win32":
dir = Path(Path.home(), "AppData", "Roaming", "Claude")
case "darwin":
dir = Path(Path.home(), "Library", "Application Support", "Claude")
return dir / "claude_desktop_config.json"
@tc.command("list", help="Show default configuration, if it exists")
def show_default_config(
show_filename: Annotated[
bool, Option(help="Show the filename for default config file")
] = False,
type: Annotated[
Optional[ConfigTypes],
Option(help="The type of configuration to show", show_default=True),
] = ConfigTypes.dremioai,
):
match type:
case ConfigTypes.dremioai:
dc = settings.default_config()
pp(f"Default config file: {dc!s} (exists = {dc.exists()!s})")
if not show_filename:
settings.configure(dc)
pp(
dump(
settings.instance().model_dump(
exclude_none=True,
mode="json",
exclude_unset=True,
by_alias=True,
)
)
)
case ConfigTypes.claude:
cc = get_claude_config_path()
pp(f"Default config file: '{cc!s}' (exists = {cc.exists()!s})")
if not show_filename:
jdump(load(cc.open()), sys.stdout, indent=2)
cc = Typer(
context_settings=dict(help_option_names=["-h", "--help"]),
name="create",
help="Create DremioAI or LLM configuration files",
)
tc.add_typer(cc)
def create_default_mcpserver_config() -> Dict[str, Any]:
if (uv := which("uv")) is not None:
uv = Path(uv).resolve()
dir = str(Path(os.getcwd()).resolve())
return {
"command": str(uv),
"args": ["run", "--directory", dir, "dremio-mcp-server", "run"],
}
else:
raise FileNotFoundError("uv command not found. Please install uv")
def create_default_config_helper(dry_run: bool):
cc = get_claude_config_path()
dcmp = {"Dremio": create_default_mcpserver_config()}
c = load(cc.open()) if cc.exists() else {"mcpServers": {}}
c.setdefault("mcpServers", {}).update(dcmp)
if dry_run:
pp(c)
return
if not cc.exists():
cc.parent.mkdir(parents=True, exist_ok=True)
with cc.open("w") as f:
jdump(c, f)
pp(f"Created default config file: {cc!s}")
@cc.command("claude", help="Create a default configuration file for Claude")
def create_default_config(
dry_run: Annotated[
bool, Option(help="Dry run, do not overwrite the config file. Just print it")
] = False,
):
create_default_config_helper(dry_run)
@cc.command("dremioai", help="Create a default configuration file")
def create_default_config(
uri: Annotated[
str,
Option(
help=f"The Dremio URL or shorthand for Dremio Cloud regions ({ ','.join(settings.DremioCloudUri)})"
),
],
pat: Annotated[
Optional[str],
Option(
help="The Dremio PAT. If it starts with @ then treat the rest is treated as a filename. Cannot be used with --username/--password."
),
] = None,
username: Annotated[
Optional[str],
Option(
help="Dremio username for authentication. Must be used with --password. Cannot be used with --pat."
),
] = None,
password: Annotated[
Optional[str],
Option(
help="Dremio password for authentication. Must be used with --username. Cannot be used with --pat."
),
] = None,
project_id: Annotated[
Optional[str],
Option(help="The Dremio project id, only if connecting to Dremio Cloud"),
] = None,
mode: Annotated[
Optional[List[str]],
Option("-m", "--mode", help="MCP server mode", click_type=Choice(_mode())),
] = [tools.ToolType.FOR_DATA_PATTERNS.name],
enable_experimental: Annotated[
bool, Option(help="Enable experimental features")
] = False,
oauth_client_id: Annotated[
Optional[str],
Option(help="The ID of OAuth application, for OAuth2 logon support"),
] = None,
dry_run: Annotated[
bool, Option(help="Dry run, do not overwrite the config file. Just print it")
] = False,
):
# Validate authentication method
has_pat = pat is not None
has_user_pass = username is not None and password is not None
if not has_pat and not has_user_pass:
raise BadParameter("Either --pat or both --username and --password must be provided")
if has_pat and has_user_pass:
raise BadParameter("Cannot specify both --pat and --username/--password authentication methods")
if (username is None) != (password is None):
raise BadParameter("Both --username and --password must be provided together")
mode = "|".join([tools.ToolType[m.upper()].name for m in mode])
dremio_config = {
"uri": uri,
"project_id": project_id,
"enable_experimental": enable_experimental,
"oauth": (
settings.OAuth2.model_validate({"client_id": oauth_client_id})
if oauth_client_id
else None
),
}
# Add authentication method
if has_pat:
dremio_config["pat"] = pat
else:
dremio_config["username"] = username
dremio_config["password"] = password
dremio = settings.Dremio.model_validate(dremio_config)
ts = settings.Tools.model_validate({"server_mode": mode})
settings.configure(settings.default_config(), force=True)
settings.instance().dremio = dremio
settings.instance().tools = ts
if (d := settings.write_settings(dry_run=dry_run)) is not None and dry_run:
pp(d)
elif not dry_run:
pp(f"Created default config file: {settings.default_config()!s}")
# --------------------------------------------------------------------------------
# testing support
tl = Typer(
context_settings=dict(help_option_names=["-h", "--help"]),
name="tools",
help="Support for testing tools directly",
)
# tl.add_typer(call)
@tl.command(
name="list",
help="List the available tools",
context_settings=dict(help_option_names=["-h", "--help"]),
)
def tools_list(
mode: Annotated[
Optional[List[str]],
Option("-m", "--mode", help="MCP server mode", click_type=Choice(_mode())),
] = [tools.ToolType.FOR_SELF.name],
):
mode = reduce(ior, [tools.ToolType[m.upper()] for m in mode])
tab = table.Table(
table.Column("Tool", justify="left", style="cyan"),
"Description",
"For",
title="Tools list",
show_lines=True,
)
for tool in tools.get_tools(For=mode):
For = tools.get_for(tool)
try:
tab.add_row(tool.__name__, tool.invoke.__doc__.strip(), For.name)
except Exception as e:
tab.add_row(tool.__name__, "No Description", For.name)
console.Console().print(tab)
@tl.command(
name="invoke",
help="Execute an available tools",
context_settings=dict(help_option_names=["-h", "--help"]),
)
def tools_exec(
tool: Annotated[str, Option("-t", "--tool", help="The tool to execute")],
config_file: Annotated[
Optional[Path],
Option("-c", "--cfg", help="The config yaml for various options"),
] = None,
args: Annotated[
Optional[List[str]],
Argument(help="The arguments to pass to the tool (arg=value ...)"),
] = None,
):
def _to_kw(arg: str) -> Tuple[str, str]:
if "=" not in arg:
raise BadParameter(f"Argument {arg} is not in the form arg=value")
return tuple(arg.split("=", 1))
settings.configure(config_file)
if args is None:
args = {}
elif type(args) == str:
args = [args]
args = dict(map(_to_kw, args))
for_all = reduce(ior, tools.ToolType.__members__.values())
all_tools = {t.__name__: t for t in tools.get_tools(for_all)}
if selected := all_tools.get(tool):
tool_instance = selected() # get arguments from settings
result = asyncio.run(tool_instance.invoke(**args))
pp(result)
else:
raise BadParameter(f"Tool {tool} not found")
ty.add_typer(tl)
ty.add_typer(tc)
def cli():
ty()
if __name__ == "__main__":
cli()