@aws/nx-plugin-mcp 1.0.0-rc.73 → 1.0.0-rc.75
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.
|
@@ -5,7 +5,7 @@ description: Key concepts.
|
|
|
5
5
|
import Link from '@components/link.astro';
|
|
6
6
|
import FrameworkLogos from '@components/framework-logos.astro';
|
|
7
7
|
|
|
8
|
-
The `@aws/nx-plugin` is an [Nx](https://nx.dev/) plugin that provides a toolkit for building and deploying full-stack applications on AWS. It gives you a collection of [Generators](#generators) which scaffold
|
|
8
|
+
The `@aws/nx-plugin` is an [Nx](https://nx.dev/) plugin that provides a toolkit for building and deploying full-stack applications on AWS. It gives you a collection of [Generators](#generators) which scaffold best-practice application code _and_ the infrastructure to deploy it — type-safe, locally runnable, and deployable from the start, getting you closer to production.
|
|
9
9
|
|
|
10
10
|
Rather than starting from a blank page, you (or your AI agent) pick the components you need — APIs, websites, authentication, AI agents, infrastructure — provide a few configuration options, and the plugin generates best-practice starter code. It even wires projects together for you (including updating existing files via AST transforms) to produce type-safe clients between your frontend and backend.
|
|
11
11
|
|
|
@@ -452,6 +452,8 @@ The `py#agent` generates these files:
|
|
|
452
452
|
- main.py entrypoint for your agent in Bedrock AgentCore Runtime
|
|
453
453
|
- agent.py defines an example agent and tools
|
|
454
454
|
- session.py resolves a SessionManager for persisting conversation state
|
|
455
|
+
- middleware/
|
|
456
|
+
- session_id_middleware.py binds the inbound AgentCore session ID for the request
|
|
455
457
|
- Dockerfile defines the docker image for deployment to AgentCore Runtime
|
|
456
458
|
- common/constructs/
|
|
457
459
|
- src
|
|
@@ -492,6 +494,29 @@ Refer to tools as your 'spellbook'.
|
|
|
492
494
|
|
|
493
495
|
This creates an example Strands agent and defines a subtraction tool. `log_model_errors` and `log_tool_errors` are hooks from the shared `dungeon_adventure_agent_connection` project that log model/tool failures instead of letting them fail silently.
|
|
494
496
|
|
|
497
|
+
```python
|
|
498
|
+
# agent/middleware/session_id_middleware.py
|
|
499
|
+
import uuid
|
|
500
|
+
|
|
501
|
+
from fastapi import Request
|
|
502
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
503
|
+
|
|
504
|
+
from dungeon_adventure_agent_connection import session_id_context
|
|
505
|
+
|
|
506
|
+
SESSION_ID_HEADER = "x-amzn-bedrock-agentcore-runtime-session-id"
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
class SessionIdMiddleware(BaseHTTPMiddleware):
|
|
510
|
+
"""Bind the session ID for this request so downstream MCP / A2A clients forward it on outbound calls."""
|
|
511
|
+
|
|
512
|
+
async def dispatch(self, request: Request, call_next):
|
|
513
|
+
session_id = request.headers.get(SESSION_ID_HEADER) or str(uuid.uuid4())
|
|
514
|
+
with session_id_context(session_id):
|
|
515
|
+
return await call_next(request)
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
The `SessionIdMiddleware` binds the inbound AgentCore runtime session ID onto a `ContextVar` for the duration of the request.
|
|
519
|
+
|
|
495
520
|
```python
|
|
496
521
|
# agent/main.py
|
|
497
522
|
import logging
|
|
@@ -505,15 +530,13 @@ from dungeon_adventure_agent_connection import get_current_session_id, session_i
|
|
|
505
530
|
from fastapi import FastAPI, Request
|
|
506
531
|
from fastapi.middleware.cors import CORSMiddleware
|
|
507
532
|
from fastapi.responses import StreamingResponse
|
|
508
|
-
from starlette.middleware.base import BaseHTTPMiddleware
|
|
509
533
|
|
|
510
534
|
from .agent import get_agent
|
|
535
|
+
from .middleware.session_id_middleware import SESSION_ID_HEADER, SessionIdMiddleware
|
|
511
536
|
from .session import get_session_manager
|
|
512
537
|
|
|
513
538
|
logging.basicConfig(level=logging.INFO)
|
|
514
539
|
|
|
515
|
-
SESSION_ID_HEADER = "x-amzn-bedrock-agentcore-runtime-session-id"
|
|
516
|
-
|
|
517
540
|
|
|
518
541
|
@asynccontextmanager
|
|
519
542
|
async def lifespan(app: FastAPI):
|
|
@@ -529,15 +552,6 @@ async def lifespan(app: FastAPI):
|
|
|
529
552
|
yield
|
|
530
553
|
|
|
531
554
|
|
|
532
|
-
class _SessionIdMiddleware(BaseHTTPMiddleware):
|
|
533
|
-
"""Bind the session ID for this request so downstream MCP / A2A clients forward it on outbound calls."""
|
|
534
|
-
|
|
535
|
-
async def dispatch(self, request: Request, call_next):
|
|
536
|
-
session_id = request.headers.get(SESSION_ID_HEADER) or str(uuid.uuid4())
|
|
537
|
-
with session_id_context(session_id):
|
|
538
|
-
return await call_next(request)
|
|
539
|
-
|
|
540
|
-
|
|
541
555
|
app = FastAPI(title="AWS Strands - StoryAgent", lifespan=lifespan)
|
|
542
556
|
app.add_middleware(
|
|
543
557
|
CORSMiddleware,
|
|
@@ -546,7 +560,7 @@ app.add_middleware(
|
|
|
546
560
|
allow_methods=["*"],
|
|
547
561
|
allow_headers=["*"],
|
|
548
562
|
)
|
|
549
|
-
app.add_middleware(
|
|
563
|
+
app.add_middleware(SessionIdMiddleware)
|
|
550
564
|
|
|
551
565
|
|
|
552
566
|
@app.post("/invocations")
|
|
@@ -589,7 +603,7 @@ async def ping():
|
|
|
589
603
|
return {"status": "healthy"}
|
|
590
604
|
```
|
|
591
605
|
|
|
592
|
-
This is the entrypoint for the agent. Because we selected `--protocol=ag-ui`, the generator wraps our Strands `Agent` with `StrandsAgent` from [`ag_ui_strands`](https://docs.copilotkit.ai/aws-strands/integration) and mounts it on a FastAPI app that speaks the [AG-UI protocol](https://docs.copilotkit.ai/aws-strands/protocol) — this is what CopilotKit will talk to from the React website. The agent is built inside a `lifespan` handler — not at import time — so container startup, not module import, owns construction, and each AgentCore session gets its own container. The `
|
|
606
|
+
This is the entrypoint for the agent. Because we selected `--protocol=ag-ui`, the generator wraps our Strands `Agent` with `StrandsAgent` from [`ag_ui_strands`](https://docs.copilotkit.ai/aws-strands/integration) and mounts it on a FastAPI app that speaks the [AG-UI protocol](https://docs.copilotkit.ai/aws-strands/protocol) — this is what CopilotKit will talk to from the React website. The agent is built inside a `lifespan` handler — not at import time — so container startup, not module import, owns construction, and each AgentCore session gets its own container. The `SessionIdMiddleware` we saw above forwards the inbound AgentCore runtime session ID so any downstream MCP/A2A client we wire up later (e.g. the Inventory MCP server in <Link path="get_started/tutorials/dungeon-game/2">Module 2</Link>) automatically forwards it on its outbound calls. Since AG-UI caches one Strands agent per `thread_id`, we plug in a `session_manager_provider` rather than the template agent's own session manager — this gives each thread its own `SessionManager` so conversation history persists across turns. That `get_session_manager()` function comes from a generated `session.py` sibling: deployed, it returns a `strands.session.S3SessionManager` backed by an S3 bucket the generator provisions automatically; under `agent-dev` (`LOCAL_DEV=true`) it always returns a `FileSessionManager` writing to a local temp directory instead, regardless of deployed configuration.
|
|
593
607
|
|
|
594
608
|
```ts
|
|
595
609
|
// common/constructs/src/app/agents/story-agent/story-agent.ts
|
package/docs/guides/py-agent.mdx
CHANGED
|
@@ -51,6 +51,9 @@ The generator will add the following files to your existing Python project. The
|
|
|
51
51
|
- init.py FastAPI application setup with CORS and error handling middleware
|
|
52
52
|
- agent.py Main agent definition with sample tools
|
|
53
53
|
- session.py Resolves the framework-specific session persistence implementation
|
|
54
|
+
- middleware/
|
|
55
|
+
- \_\_init\_\_.py Python package initialization
|
|
56
|
+
- session_id_middleware.py Binds the inbound AgentCore session ID for the request
|
|
54
57
|
- main.py FastAPI entry point for Bedrock AgentCore Runtime
|
|
55
58
|
- Dockerfile Entry point for hosting your agent (excluded when `infra` is set to `None`)
|
|
56
59
|
- pyproject.toml Updated with Strands dependencies
|
|
@@ -70,6 +73,9 @@ The entry point exposes your agent over the A2A protocol (Strands uses the [Stra
|
|
|
70
73
|
- \_\_init\_\_.py Python package initialization
|
|
71
74
|
- agent.py Main agent definition with sample tools
|
|
72
75
|
- session.py Resolves the framework-specific session persistence implementation
|
|
76
|
+
- middleware/
|
|
77
|
+
- \_\_init\_\_.py Python package initialization
|
|
78
|
+
- session_id_middleware.py Binds the inbound AgentCore session ID for the request
|
|
73
79
|
- main.py A2A server entry point
|
|
74
80
|
- Dockerfile Entry point for hosting your agent (excluded when `infra` is set to `None`)
|
|
75
81
|
- pyproject.toml Updated with framework and A2A dependencies
|
|
@@ -89,6 +95,9 @@ The entry point exposes your agent via the [AG-UI](https://docs.ag-ui.com/) prot
|
|
|
89
95
|
- \_\_init\_\_.py Python package initialization
|
|
90
96
|
- agent.py Main agent definition with sample tools
|
|
91
97
|
- session.py Resolves the framework-specific session persistence implementation
|
|
98
|
+
- middleware/
|
|
99
|
+
- \_\_init\_\_.py Python package initialization
|
|
100
|
+
- session_id_middleware.py Binds the inbound AgentCore session ID for the request
|
|
92
101
|
- main.py AG-UI server entry point
|
|
93
102
|
- Dockerfile Entry point for hosting your agent (excluded when `infra` is set to `None`)
|
|
94
103
|
- pyproject.toml Updated with framework and AG-UI dependencies
|