specstream-agentcore-mcp 1.0.0__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.
- specstream_agentcore_mcp/__init__.py +5 -0
- specstream_agentcore_mcp/__main__.py +3 -0
- specstream_agentcore_mcp/server.py +226 -0
- specstream_agentcore_mcp-1.0.0.dist-info/METADATA +53 -0
- specstream_agentcore_mcp-1.0.0.dist-info/RECORD +8 -0
- specstream_agentcore_mcp-1.0.0.dist-info/WHEEL +4 -0
- specstream_agentcore_mcp-1.0.0.dist-info/entry_points.txt +2 -0
- specstream_agentcore_mcp-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""SpecStream's MCP server in the shape Amazon Bedrock AgentCore Runtime runs.
|
|
2
|
+
|
|
3
|
+
AgentCore Runtime starts the container and sends MCP as stateless Streamable HTTP to
|
|
4
|
+
0.0.0.0:8000/mcp, with its own `Mcp-Session-Id` per runtime session. This server answers
|
|
5
|
+
there and forwards each JSON-RPC message to SpecStream's hosted MCP server
|
|
6
|
+
(https://api.specstream.ai/mcp), with the key from `SPECSTREAM_API_KEY` (set as an
|
|
7
|
+
environment variable of the agent runtime). It keeps one upstream session per runtime
|
|
8
|
+
session, and opens it itself when a client calls a tool without `initialize` first.
|
|
9
|
+
|
|
10
|
+
Without a key it still answers `initialize` and `tools/list`, so the AgentCore console and a
|
|
11
|
+
gateway can list the tools, and each tool call says how to get a key.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
import threading
|
|
20
|
+
from collections.abc import Mapping
|
|
21
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from specstream_mcp_stdio.proxy import (
|
|
25
|
+
MISSING_KEY,
|
|
26
|
+
PROTOCOL_VERSION,
|
|
27
|
+
_keyless_reply,
|
|
28
|
+
_mcp_url,
|
|
29
|
+
_post_json,
|
|
30
|
+
_stateless_version,
|
|
31
|
+
authorization_from_env,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
HOST = "0.0.0.0"
|
|
35
|
+
PORT = 8000
|
|
36
|
+
PATH = "/mcp"
|
|
37
|
+
MAX_BODY = 4 * 1024 * 1024
|
|
38
|
+
CLIENT_INFO = {"name": "specstream-agentcore", "version": "1.0.0"}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Upstream:
|
|
42
|
+
"""One upstream MCP session per AgentCore runtime session id."""
|
|
43
|
+
|
|
44
|
+
def __init__(self, url: str, authorization: str | None) -> None:
|
|
45
|
+
self.url = url
|
|
46
|
+
self.authorization = authorization
|
|
47
|
+
self._sessions: dict[str, tuple[str | None, str]] = {}
|
|
48
|
+
self._lock = threading.Lock()
|
|
49
|
+
|
|
50
|
+
def forget(self, key: str) -> None:
|
|
51
|
+
with self._lock:
|
|
52
|
+
self._sessions.pop(key, None)
|
|
53
|
+
|
|
54
|
+
def _open(self, key: str) -> tuple[str | None, str]:
|
|
55
|
+
assert self.authorization is not None
|
|
56
|
+
hello = {
|
|
57
|
+
"jsonrpc": "2.0",
|
|
58
|
+
"id": "agentcore-init",
|
|
59
|
+
"method": "initialize",
|
|
60
|
+
"params": {
|
|
61
|
+
"protocolVersion": PROTOCOL_VERSION,
|
|
62
|
+
"capabilities": {},
|
|
63
|
+
"clientInfo": CLIENT_INFO,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
_, headers, body = _post_json(
|
|
67
|
+
self.url, hello, authorization=self.authorization, session_id=None
|
|
68
|
+
)
|
|
69
|
+
version = ((body or {}).get("result") or {}).get("protocolVersion") or PROTOCOL_VERSION
|
|
70
|
+
session_id = headers.get("mcp-session-id")
|
|
71
|
+
_post_json(
|
|
72
|
+
self.url,
|
|
73
|
+
{"jsonrpc": "2.0", "method": "notifications/initialized"},
|
|
74
|
+
authorization=self.authorization,
|
|
75
|
+
session_id=session_id,
|
|
76
|
+
session_version=version,
|
|
77
|
+
)
|
|
78
|
+
state = (session_id, version)
|
|
79
|
+
with self._lock:
|
|
80
|
+
self._sessions[key] = state
|
|
81
|
+
return state
|
|
82
|
+
|
|
83
|
+
def handle(self, key: str, message: Mapping[str, Any]) -> tuple[int, dict[str, Any] | None]:
|
|
84
|
+
"""Answer one JSON-RPC message: (HTTP status, body or None for 202)."""
|
|
85
|
+
if self.authorization is None:
|
|
86
|
+
reply = _keyless_reply(message)
|
|
87
|
+
return (200, reply) if reply is not None else (202, None)
|
|
88
|
+
method = message.get("method")
|
|
89
|
+
if method == "notifications/cancelled" and "id" not in message:
|
|
90
|
+
return 202, None
|
|
91
|
+
if _stateless_version(message):
|
|
92
|
+
status, _, body = _post_json(
|
|
93
|
+
self.url, dict(message), authorization=self.authorization, session_id=None
|
|
94
|
+
)
|
|
95
|
+
return (status, body) if body is not None else (202, None)
|
|
96
|
+
if method == "initialize":
|
|
97
|
+
status, headers, body = _post_json(
|
|
98
|
+
self.url, dict(message), authorization=self.authorization, session_id=None
|
|
99
|
+
)
|
|
100
|
+
version = ((body or {}).get("result") or {}).get("protocolVersion") or PROTOCOL_VERSION
|
|
101
|
+
with self._lock:
|
|
102
|
+
self._sessions[key] = (headers.get("mcp-session-id"), version)
|
|
103
|
+
return status, body
|
|
104
|
+
with self._lock:
|
|
105
|
+
state = self._sessions.get(key)
|
|
106
|
+
if method == "notifications/initialized" and state is None:
|
|
107
|
+
return 202, None
|
|
108
|
+
if state is None:
|
|
109
|
+
state = self._open(key)
|
|
110
|
+
try:
|
|
111
|
+
status, body = self._send(message, state)
|
|
112
|
+
except ValueError as exc:
|
|
113
|
+
if str(exc) != "mcp http 404" or "id" not in message:
|
|
114
|
+
raise
|
|
115
|
+
# The upstream session expired: open a new one and send the message again.
|
|
116
|
+
status, body = self._send(message, self._open(key))
|
|
117
|
+
return (status, body) if body is not None else (202, None)
|
|
118
|
+
|
|
119
|
+
def _send(
|
|
120
|
+
self, message: Mapping[str, Any], state: tuple[str | None, str]
|
|
121
|
+
) -> tuple[int, dict[str, Any] | None]:
|
|
122
|
+
assert self.authorization is not None
|
|
123
|
+
status, _, body = _post_json(
|
|
124
|
+
self.url,
|
|
125
|
+
dict(message),
|
|
126
|
+
authorization=self.authorization,
|
|
127
|
+
session_id=state[0],
|
|
128
|
+
session_version=state[1],
|
|
129
|
+
)
|
|
130
|
+
return status, body
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _error(request_id: Any, code: int, text: str) -> dict[str, Any]:
|
|
134
|
+
return {"jsonrpc": "2.0", "id": request_id, "error": {"code": code, "message": text}}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def make_handler(upstream: Upstream) -> type[BaseHTTPRequestHandler]:
|
|
138
|
+
class Handler(BaseHTTPRequestHandler):
|
|
139
|
+
protocol_version = "HTTP/1.1"
|
|
140
|
+
server_version = "SpecStreamAgentCore/1.0"
|
|
141
|
+
|
|
142
|
+
def log_message(self, format: str, *args: Any) -> None: # noqa: A002
|
|
143
|
+
sys.stderr.write("%s %s\n" % (self.command, self.path.split("?", 1)[0]))
|
|
144
|
+
|
|
145
|
+
def _send(self, status: int, body: Any = None) -> None:
|
|
146
|
+
raw = b"" if body is None else json.dumps(body, separators=(",", ":")).encode()
|
|
147
|
+
self.send_response(status)
|
|
148
|
+
if body is not None:
|
|
149
|
+
self.send_header("Content-Type", "application/json")
|
|
150
|
+
self.send_header("Content-Length", str(len(raw)))
|
|
151
|
+
self.end_headers()
|
|
152
|
+
if raw:
|
|
153
|
+
self.wfile.write(raw)
|
|
154
|
+
|
|
155
|
+
def do_GET(self) -> None: # noqa: N802
|
|
156
|
+
route = self.path.split("?", 1)[0]
|
|
157
|
+
if route == "/ping":
|
|
158
|
+
self._send(200, {"status": "Healthy"})
|
|
159
|
+
elif route == PATH:
|
|
160
|
+
# Stateless server: no server-to-client stream.
|
|
161
|
+
self._send(405, {"error": "method not allowed"})
|
|
162
|
+
else:
|
|
163
|
+
self._send(404, {"error": "not found"})
|
|
164
|
+
|
|
165
|
+
def do_DELETE(self) -> None: # noqa: N802
|
|
166
|
+
if self.path.split("?", 1)[0] != PATH:
|
|
167
|
+
self._send(404, {"error": "not found"})
|
|
168
|
+
return
|
|
169
|
+
upstream.forget(self.headers.get("Mcp-Session-Id") or "")
|
|
170
|
+
self._send(200)
|
|
171
|
+
|
|
172
|
+
def do_POST(self) -> None: # noqa: N802
|
|
173
|
+
if self.path.split("?", 1)[0] != PATH:
|
|
174
|
+
self._send(404, {"error": "not found"})
|
|
175
|
+
return
|
|
176
|
+
length = int(self.headers.get("Content-Length") or 0)
|
|
177
|
+
if length <= 0 or length > MAX_BODY:
|
|
178
|
+
self._send(400, _error(None, -32600, "a JSON-RPC body is required"))
|
|
179
|
+
return
|
|
180
|
+
try:
|
|
181
|
+
message = json.loads(self.rfile.read(length))
|
|
182
|
+
except (json.JSONDecodeError, UnicodeDecodeError):
|
|
183
|
+
self._send(400, _error(None, -32700, "parse error"))
|
|
184
|
+
return
|
|
185
|
+
if not isinstance(message, dict):
|
|
186
|
+
self._send(400, _error(None, -32600, "send one JSON-RPC message per request"))
|
|
187
|
+
return
|
|
188
|
+
key = self.headers.get("Mcp-Session-Id") or ""
|
|
189
|
+
try:
|
|
190
|
+
status, body = upstream.handle(key, message)
|
|
191
|
+
except (ValueError, OSError) as exc:
|
|
192
|
+
if "id" not in message:
|
|
193
|
+
self._send(202)
|
|
194
|
+
return
|
|
195
|
+
self._send(502, _error(message.get("id"), -32603, f"SpecStream MCP failed: {exc}"))
|
|
196
|
+
return
|
|
197
|
+
self._send(status, body)
|
|
198
|
+
|
|
199
|
+
return Handler
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def make_server(
|
|
203
|
+
env: Mapping[str, str] | None = None, host: str = HOST, port: int = PORT
|
|
204
|
+
) -> ThreadingHTTPServer:
|
|
205
|
+
environ = env if env is not None else os.environ
|
|
206
|
+
url = _mcp_url(environ)
|
|
207
|
+
try:
|
|
208
|
+
authorization: str | None = authorization_from_env(environ)
|
|
209
|
+
except KeyError:
|
|
210
|
+
authorization = None
|
|
211
|
+
print(MISSING_KEY, file=sys.stderr)
|
|
212
|
+
return ThreadingHTTPServer((host, port), make_handler(Upstream(url, authorization)))
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def main() -> int:
|
|
216
|
+
try:
|
|
217
|
+
server = make_server(port=int(os.environ.get("PORT") or PORT))
|
|
218
|
+
except ValueError as exc:
|
|
219
|
+
print(str(exc), file=sys.stderr)
|
|
220
|
+
return 2
|
|
221
|
+
print(f"SpecStream MCP for AgentCore on {HOST}:{server.server_address[1]}{PATH}", file=sys.stderr)
|
|
222
|
+
try:
|
|
223
|
+
server.serve_forever()
|
|
224
|
+
except KeyboardInterrupt:
|
|
225
|
+
pass
|
|
226
|
+
return 0
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: specstream-agentcore-mcp
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: SpecStream's MCP server packaged for Amazon Bedrock AgentCore Runtime.
|
|
5
|
+
Project-URL: Homepage, https://specstream.ai
|
|
6
|
+
Project-URL: Documentation, https://specstream.ai/integrations/amazon-bedrock-agentcore
|
|
7
|
+
Project-URL: Repository, https://github.com/specstream/specstream-tools
|
|
8
|
+
Author-email: SpecStream <support@specstream.ai>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agentcore,aws,bedrock,mcp,specstream,web-data,web-search
|
|
12
|
+
Requires-Python: >=3.12
|
|
13
|
+
Requires-Dist: specstream-mcp-stdio<2,>=1.2.5
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# SpecStream MCP server for Amazon Bedrock AgentCore
|
|
17
|
+
|
|
18
|
+
SpecStream, the live web data engine, as an MCP server that Amazon Bedrock AgentCore Runtime hosts.
|
|
19
|
+
Agents ask for data in plain words and get structured rows with a source URL for each value.
|
|
20
|
+
|
|
21
|
+
The container listens on `0.0.0.0:8000/mcp` (stateless Streamable HTTP, as AgentCore Runtime expects)
|
|
22
|
+
and answers `GET /ping`. It forwards each MCP message to `https://api.specstream.ai/mcp` with the key
|
|
23
|
+
in `SPECSTREAM_API_KEY`. Create a key under Agents & API at https://specstream.ai/workspace.
|
|
24
|
+
New accounts get $10 of credit, with no card.
|
|
25
|
+
|
|
26
|
+
## Deploy on AgentCore Runtime
|
|
27
|
+
|
|
28
|
+
1. Build the image for arm64 and push it to your Amazon ECR repository:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
docker buildx build --platform linux/arm64 -t <account>.dkr.ecr.us-east-1.amazonaws.com/specstream-mcp:1.0.0 --push .
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
2. Create an IAM role that `bedrock-agentcore.amazonaws.com` can assume.
|
|
35
|
+
|
|
36
|
+
3. Create the agent runtime:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
aws bedrock-agentcore-control create-agent-runtime \
|
|
40
|
+
--region us-east-1 \
|
|
41
|
+
--agent-runtime-name specstream_mcp \
|
|
42
|
+
--agent-runtime-artifact '{"containerConfiguration":{"containerUri":"<account>.dkr.ecr.us-east-1.amazonaws.com/specstream-mcp:1.0.0"}}' \
|
|
43
|
+
--role-arn <role ARN> \
|
|
44
|
+
--network-configuration '{"networkMode":"PUBLIC"}' \
|
|
45
|
+
--protocol-configuration '{"serverProtocol":"MCP"}' \
|
|
46
|
+
--environment-variables '{"SPECSTREAM_API_KEY":"<your SpecStream API key>"}'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
4. Call it with `aws bedrock-agentcore invoke-agent-runtime`, or add the runtime as a target of an
|
|
50
|
+
AgentCore Gateway.
|
|
51
|
+
|
|
52
|
+
Without a key the server still answers `initialize` and `tools/list`, and each tool call says how to
|
|
53
|
+
get a key.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
specstream_agentcore_mcp/__init__.py,sha256=ENoGsj7RaGpOwizvVyW1iQxx2PeJvaKhxtMWxvdj_8I,164
|
|
2
|
+
specstream_agentcore_mcp/__main__.py,sha256=Hsh3MuzotIeyM-Rh8E38GIQb2I-XtNmdDhXQxIfkfBs,75
|
|
3
|
+
specstream_agentcore_mcp/server.py,sha256=rmA3-9bpiXAdgxteb-dK434FA_mVqu4v9d0Sccf9FSI,8774
|
|
4
|
+
specstream_agentcore_mcp-1.0.0.dist-info/METADATA,sha256=kjaihN5KySSTnRjF8A5x2twr9ebFQSmjk5-vjRPWEy0,2316
|
|
5
|
+
specstream_agentcore_mcp-1.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
6
|
+
specstream_agentcore_mcp-1.0.0.dist-info/entry_points.txt,sha256=tmYUryD8oeeU6o49h23pssGyy6-gSlx68ltdVWIDyOQ,82
|
|
7
|
+
specstream_agentcore_mcp-1.0.0.dist-info/licenses/LICENSE,sha256=lvzGtfzsxc8Xe1_0sabdiWTcSDiSg3jqaFKV9x3F4oo,1071
|
|
8
|
+
specstream_agentcore_mcp-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SpecStream SAS
|
|
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.
|