mcp-proxy 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcp_proxy/__init__.py +143 -0
- mcp_proxy/__main__.py +30 -0
- mcp_proxy/py.typed +0 -0
- mcp_proxy-0.2.1.dist-info/LICENSE +21 -0
- mcp_proxy-0.2.1.dist-info/METADATA +7 -0
- mcp_proxy-0.2.1.dist-info/RECORD +9 -0
- mcp_proxy-0.2.1.dist-info/WHEEL +5 -0
- mcp_proxy-0.2.1.dist-info/entry_points.txt +2 -0
- mcp_proxy-0.2.1.dist-info/top_level.txt +1 -0
mcp_proxy/__init__.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Create a local server that proxies requests to a remote server over SSE."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import typing as t
|
|
5
|
+
|
|
6
|
+
from mcp import server, types
|
|
7
|
+
from mcp.client.session import ClientSession
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def create_proxy_server(remote_app: ClientSession) -> server.Server: # noqa: C901
|
|
13
|
+
"""Create a server instance from a remote app."""
|
|
14
|
+
response = await remote_app.initialize()
|
|
15
|
+
capabilities = response.capabilities
|
|
16
|
+
|
|
17
|
+
app = server.Server(response.serverInfo.name)
|
|
18
|
+
|
|
19
|
+
if capabilities.prompts:
|
|
20
|
+
|
|
21
|
+
async def _list_prompts(_: t.Any) -> types.ServerResult: # noqa: ANN401
|
|
22
|
+
result = await remote_app.list_prompts()
|
|
23
|
+
return types.ServerResult(result)
|
|
24
|
+
|
|
25
|
+
app.request_handlers[types.ListPromptsRequest] = _list_prompts
|
|
26
|
+
|
|
27
|
+
async def _get_prompt(req: types.GetPromptRequest) -> types.ServerResult:
|
|
28
|
+
result = await remote_app.get_prompt(req.params.name, req.params.arguments)
|
|
29
|
+
return types.ServerResult(result)
|
|
30
|
+
|
|
31
|
+
app.request_handlers[types.GetPromptRequest] = _get_prompt
|
|
32
|
+
|
|
33
|
+
if capabilities.resources:
|
|
34
|
+
|
|
35
|
+
async def _list_resources(_: t.Any) -> types.ServerResult: # noqa: ANN401
|
|
36
|
+
result = await remote_app.list_resources()
|
|
37
|
+
return types.ServerResult(result)
|
|
38
|
+
|
|
39
|
+
app.request_handlers[types.ListResourcesRequest] = _list_resources
|
|
40
|
+
|
|
41
|
+
# list_resource_templates() is not implemented in the client
|
|
42
|
+
# async def _list_resource_templates(_: t.Any) -> types.ServerResult:
|
|
43
|
+
# result = await remote_app.list_resource_templates()
|
|
44
|
+
# return types.ServerResult(result)
|
|
45
|
+
|
|
46
|
+
# app.request_handlers[types.ListResourceTemplatesRequest] = _list_resource_templates
|
|
47
|
+
|
|
48
|
+
async def _read_resource(req: types.ReadResourceRequest) -> types.ServerResult:
|
|
49
|
+
result = await remote_app.read_resource(req.params.uri)
|
|
50
|
+
return types.ServerResult(result)
|
|
51
|
+
|
|
52
|
+
app.request_handlers[types.ReadResourceRequest] = _read_resource
|
|
53
|
+
|
|
54
|
+
if capabilities.logging:
|
|
55
|
+
|
|
56
|
+
async def _set_logging_level(req: types.SetLevelRequest) -> types.ServerResult:
|
|
57
|
+
await remote_app.set_logging_level(req.params.level)
|
|
58
|
+
return types.ServerResult(types.EmptyResult())
|
|
59
|
+
|
|
60
|
+
app.request_handlers[types.SetLevelRequest] = _set_logging_level
|
|
61
|
+
|
|
62
|
+
if capabilities.resources:
|
|
63
|
+
|
|
64
|
+
async def _subscribe_resource(req: types.SubscribeRequest) -> types.ServerResult:
|
|
65
|
+
await remote_app.subscribe_resource(req.params.uri)
|
|
66
|
+
return types.ServerResult(types.EmptyResult())
|
|
67
|
+
|
|
68
|
+
app.request_handlers[types.SubscribeRequest] = _subscribe_resource
|
|
69
|
+
|
|
70
|
+
async def _unsubscribe_resource(req: types.UnsubscribeRequest) -> types.ServerResult:
|
|
71
|
+
await remote_app.unsubscribe_resource(req.params.uri)
|
|
72
|
+
return types.ServerResult(types.EmptyResult())
|
|
73
|
+
|
|
74
|
+
app.request_handlers[types.UnsubscribeRequest] = _unsubscribe_resource
|
|
75
|
+
|
|
76
|
+
if capabilities.tools:
|
|
77
|
+
|
|
78
|
+
async def _list_tools(_: t.Any) -> types.ServerResult: # noqa: ANN401
|
|
79
|
+
tools = await remote_app.list_tools()
|
|
80
|
+
return types.ServerResult(tools)
|
|
81
|
+
|
|
82
|
+
app.request_handlers[types.ListToolsRequest] = _list_tools
|
|
83
|
+
|
|
84
|
+
async def _call_tool(req: types.CallToolRequest) -> types.ServerResult:
|
|
85
|
+
try:
|
|
86
|
+
result = await remote_app.call_tool(
|
|
87
|
+
req.params.name,
|
|
88
|
+
(req.params.arguments or {}),
|
|
89
|
+
)
|
|
90
|
+
return types.ServerResult(result)
|
|
91
|
+
except Exception as e: # noqa: BLE001
|
|
92
|
+
return types.ServerResult(
|
|
93
|
+
types.CallToolResult(
|
|
94
|
+
content=[types.TextContent(type="text", text=str(e))],
|
|
95
|
+
isError=True,
|
|
96
|
+
),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
app.request_handlers[types.CallToolRequest] = _call_tool
|
|
100
|
+
|
|
101
|
+
async def _send_progress_notification(req: types.ProgressNotification) -> None:
|
|
102
|
+
await remote_app.send_progress_notification(
|
|
103
|
+
req.params.progressToken,
|
|
104
|
+
req.params.progress,
|
|
105
|
+
req.params.total,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
app.notification_handlers[types.ProgressNotification] = _send_progress_notification
|
|
109
|
+
|
|
110
|
+
async def _complete(req: types.CompleteRequest) -> types.ServerResult:
|
|
111
|
+
result = await remote_app.complete(
|
|
112
|
+
req.params.ref,
|
|
113
|
+
req.params.argument.model_dump(),
|
|
114
|
+
)
|
|
115
|
+
return types.ServerResult(result)
|
|
116
|
+
|
|
117
|
+
app.request_handlers[types.CompleteRequest] = _complete
|
|
118
|
+
|
|
119
|
+
return app
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
async def run_sse_client(url: str, api_access_token: str | None = None) -> None:
|
|
123
|
+
"""Run the SSE client.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
url: The URL to connect to.
|
|
127
|
+
api_access_token: The API access token to use for authentication.
|
|
128
|
+
|
|
129
|
+
"""
|
|
130
|
+
from mcp.client.sse import sse_client
|
|
131
|
+
|
|
132
|
+
headers = {}
|
|
133
|
+
if api_access_token is not None:
|
|
134
|
+
headers["Authorization"] = f"Bearer {api_access_token}"
|
|
135
|
+
|
|
136
|
+
async with sse_client(url=url, headers=headers) as streams, ClientSession(*streams) as session:
|
|
137
|
+
app = await create_proxy_server(session)
|
|
138
|
+
async with server.stdio_server() as (read_stream, write_stream):
|
|
139
|
+
await app.run(
|
|
140
|
+
read_stream,
|
|
141
|
+
write_stream,
|
|
142
|
+
app.create_initialization_options(),
|
|
143
|
+
)
|
mcp_proxy/__main__.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""The entry point for the mcp-proxy application. It sets up the logging and runs the main function.
|
|
2
|
+
|
|
3
|
+
Two ways to run the application:
|
|
4
|
+
1. Run the application as a module `uv run -m mcp_proxy`
|
|
5
|
+
2. Run the application as a package `uv run mcp-proxy`
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import logging
|
|
11
|
+
import os
|
|
12
|
+
import typing as t
|
|
13
|
+
|
|
14
|
+
from . import run_sse_client
|
|
15
|
+
|
|
16
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
17
|
+
SSE_URL: t.Final[str] = os.getenv("SSE_URL", "")
|
|
18
|
+
API_ACCESS_TOKEN: t.Final[str | None] = os.getenv("API_ACCESS_TOKEN", None)
|
|
19
|
+
|
|
20
|
+
if not SSE_URL:
|
|
21
|
+
raise ValueError("SSE_URL environment variable is not set")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def main() -> None:
|
|
25
|
+
"""Start the client using asyncio."""
|
|
26
|
+
asyncio.run(run_sse_client(SSE_URL, api_access_token=API_ACCESS_TOKEN))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
if __name__ == "__main__":
|
|
30
|
+
main()
|
mcp_proxy/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Sergey Parfenyuk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
mcp_proxy/__init__.py,sha256=-vnAAZ56FcdkI1TKI5Ync8lojKHHy__s2CLWTYS7_no,5319
|
|
2
|
+
mcp_proxy/__main__.py,sha256=7IKy0orhu67F9-lsPCps9CqbZ3kVN4_i-BBAFA1t34E,770
|
|
3
|
+
mcp_proxy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
mcp_proxy-0.2.1.dist-info/LICENSE,sha256=IB5Kq1DC-6sU8yGp_Hzv6SXBKRrlSbYcKCLNkciNBUg,1073
|
|
5
|
+
mcp_proxy-0.2.1.dist-info/METADATA,sha256=60-QQSE3yg5O2IxWgt67l3CSHSgA-O5zNwWLeBUrhEc,178
|
|
6
|
+
mcp_proxy-0.2.1.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
7
|
+
mcp_proxy-0.2.1.dist-info/entry_points.txt,sha256=6wHxBfNcv1bdXrB27vVQ3RdvpGlP1a-qx3hviRXwCAo,54
|
|
8
|
+
mcp_proxy-0.2.1.dist-info/top_level.txt,sha256=Sc_nmlR8kpkoOyksgzzlMw7rdAn5ZsPfRsd_tVWgiBI,10
|
|
9
|
+
mcp_proxy-0.2.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mcp_proxy
|