-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (173 loc) · 6.63 KB
/
main.py
File metadata and controls
195 lines (173 loc) · 6.63 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
# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import logging
import os
from enum import StrEnum
from typing import Annotated
import typer
from mcp.server.fastmcp import FastMCP
from deepset_mcp.config import DEEPSET_DOCS_DEFAULT_SHARE_URL, DOCS_SEARCH_TOOL_NAME
from deepset_mcp.mcp.server import configure_mcp_server
from deepset_mcp.mcp.tool_registry import TOOL_REGISTRY
class TransportEnum(StrEnum):
"""Transport mode for the MCP server."""
STDIO = "stdio"
STREAMABLE_HTTP = "streamable-http"
SSE = "sse"
app = typer.Typer(
name="deepset-mcp",
help="Run the Deepset MCP server to interact with the deepset AI platform.",
no_args_is_help=True,
)
@app.command()
def main(
workspace: Annotated[
str | None,
typer.Option(
"--workspace",
help="Deepset workspace name. Can also be set via DEEPSET_WORKSPACE environment variable.",
),
] = None,
api_key: Annotated[
str | None,
typer.Option(
"--api-key",
help="Deepset API key for authentication. Can also be set via DEEPSET_API_KEY environment variable.",
),
] = None,
api_url: Annotated[
str | None,
typer.Option(
"--api-url",
help="Deepset API base URL. Can also be set via DEEPSET_API_URL environment variable.",
),
] = None,
docs_share_url: Annotated[
str | None,
typer.Option(
"--docs-share-url",
help="Deepset docs search share URL. Can also be set via DEEPSET_DOCS_SHARE_URL environment variable.",
),
] = None,
tools: Annotated[
list[str] | None,
typer.Option(
"--tools",
help="Space-separated list of tools to register. If not specified, all available tools will be registered.",
),
] = None,
list_tools: Annotated[
bool,
typer.Option(
"--list-tools",
help="List all available tools and exit.",
),
] = False,
api_key_from_auth_header: Annotated[
bool,
typer.Option(
"--api-key-from-auth-header/--no-api-key-from-auth-header",
help="Get the deepset API key from the request's authorization header instead of using a static key.",
),
] = False,
transport: Annotated[
TransportEnum,
typer.Option(
"--transport",
help="The type of transport to use for running the MCP server.",
),
] = TransportEnum.STDIO,
object_store_backend: Annotated[
str | None,
typer.Option(
"--object-store-backend",
help="Object store backend type: 'memory' or 'redis'. "
"Can also be set via OBJECT_STORE_BACKEND environment variable. Default is 'memory'.",
),
] = None,
object_store_redis_url: Annotated[
str | None,
typer.Option(
"--object-store-redis-url",
help="Redis connection URL (e.g., redis://localhost:6379). "
"Can also be set via OBJECT_STORE_REDIS_URL environment variable.",
),
] = None,
object_store_ttl: Annotated[
int,
typer.Option(
"--object-store-ttl",
help="TTL in seconds for stored objects. Default: 600 (10 minutes). "
"Can also be set via OBJECT_STORE_TTL environment variable.",
),
] = 600,
) -> None:
"""
Run the Deepset MCP server.
The Deepset MCP server provides tools to interact with the deepset AI platform,
allowing you to create, debug, and learn about pipelines on the platform.
:param workspace: Deepset workspace name. Pass if you only want to run the tools on a specific workspace.
:param api_key: Deepset API key for authentication
:param api_url: Deepset API base URL
:param docs_share_url: Deepset docs search share URL
:param tools: List of tools to register
:param list_tools: List all available tools and exit
:param api_key_from_auth_header: Get API key from authorization header
:param transport: Type of transport to use for the MCP server
:param object_store_backend: Object store backend type ('memory' or 'redis')
:param object_store_redis_url: Redis connection URL (required if backend='redis')
:param object_store_ttl: TTL in seconds for stored objects
"""
# Handle --list-tools flag early
if list_tools:
typer.echo("Available tools:")
for tool_name in sorted(TOOL_REGISTRY.keys()):
typer.echo(f" {tool_name}")
raise typer.Exit()
# Prefer command line arguments, fallback to environment variables
workspace = workspace or os.getenv("DEEPSET_WORKSPACE")
api_key = api_key or os.getenv("DEEPSET_API_KEY")
api_url = api_url or os.getenv("DEEPSET_API_URL")
docs_share_url = docs_share_url or os.getenv("DEEPSET_DOCS_SHARE_URL", DEEPSET_DOCS_DEFAULT_SHARE_URL)
# ObjectStore configuration
backend = str(object_store_backend or os.getenv("OBJECT_STORE_BACKEND", "memory"))
redis_url = object_store_redis_url or os.getenv("OBJECT_STORE_REDIS_URL")
ttl = int(os.getenv("OBJECT_STORE_TTL", str(object_store_ttl)))
if tools:
tool_names = set(tools)
else:
logging.info("Registering all available tools.")
tool_names = set(TOOL_REGISTRY.keys())
if api_key is None and not api_key_from_auth_header:
typer.echo(
"Error: API key is required. Either provide --api-key or use --api-key-from-auth-header "
"to fetch the API key from the authorization header.",
err=True,
)
raise typer.Exit(1)
if not workspace:
logging.info("No workspace specified. Workspace needs to be provided during tool calling.")
if DOCS_SEARCH_TOOL_NAME in tool_names and docs_share_url is None:
typer.echo(
f"Error: {DOCS_SEARCH_TOOL_NAME} tool is requested but no docs share URL provided. "
"Set --docs-share-url or DEEPSET_DOCS_SHARE_URL environment variable.",
err=True,
)
raise typer.Exit(1)
mcp = FastMCP("deepset AI platform MCP server")
configure_mcp_server(
mcp_server_instance=mcp,
deepset_api_key=api_key,
deepset_api_url=api_url,
deepset_workspace=workspace,
tools_to_register=tool_names,
deepset_docs_shareable_prototype_url=docs_share_url,
get_api_key_from_authorization_header=api_key_from_auth_header,
object_store_backend=backend,
object_store_redis_url=redis_url,
object_store_ttl=ttl,
)
mcp.run(transport=transport.value)
if __name__ == "__main__":
app()