@aws/nx-plugin-mcp 0.119.0 → 0.121.0

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.
Files changed (89) hide show
  1. package/bin/aws-nx-mcp.js +23 -35
  2. package/docs/guides/astro-docs.mdx +142 -0
  3. package/docs/guides/connection/py-agent-a2a.mdx +105 -0
  4. package/docs/guides/connection/py-agent-mcp.mdx +147 -0
  5. package/docs/guides/connection/react-agui.mdx +240 -0
  6. package/docs/guides/connection/react-fastapi.mdx +1476 -0
  7. package/docs/guides/connection/react-py-agent.mdx +198 -0
  8. package/docs/guides/connection/react-smithy.mdx +1355 -0
  9. package/docs/guides/connection/react-trpc.mdx +447 -0
  10. package/docs/guides/connection/react-ts-agent.mdx +198 -0
  11. package/docs/guides/connection/smithy-rdb.mdx +161 -0
  12. package/docs/guides/connection/trpc-rdb.mdx +127 -0
  13. package/docs/guides/connection/ts-agent-a2a.mdx +106 -0
  14. package/docs/guides/connection/ts-agent-mcp.mdx +144 -0
  15. package/docs/guides/connection/ts-agent-rdb.mdx +141 -0
  16. package/docs/guides/connection/ts-mcp-server-rdb.mdx +135 -0
  17. package/docs/guides/connection.mdx +126 -0
  18. package/docs/guides/docker-bundling.mdx +403 -0
  19. package/docs/guides/fastapi.mdx +509 -0
  20. package/docs/guides/license.mdx +293 -0
  21. package/docs/guides/nx-generator.mdx +588 -0
  22. package/docs/guides/py-agent.mdx +483 -0
  23. package/docs/guides/py-mcp-server.mdx +161 -0
  24. package/docs/guides/python-lambda-function.mdx +207 -0
  25. package/docs/guides/python-project.mdx +228 -0
  26. package/docs/guides/react-website-auth.mdx +216 -0
  27. package/docs/guides/react-website.mdx +421 -0
  28. package/docs/guides/runtime-config.mdx +312 -0
  29. package/docs/guides/terraform-project.mdx +317 -0
  30. package/docs/guides/trpc.mdx +816 -0
  31. package/docs/guides/ts-agent.mdx +436 -0
  32. package/docs/guides/ts-lambda-function.mdx +218 -0
  33. package/docs/guides/ts-mcp-server.mdx +169 -0
  34. package/docs/guides/ts-nx-plugin.mdx +159 -0
  35. package/docs/guides/ts-rdb.mdx +759 -0
  36. package/docs/guides/ts-smithy-api.mdx +661 -0
  37. package/docs/guides/typescript-infrastructure.mdx +408 -0
  38. package/docs/guides/typescript-project.mdx +312 -0
  39. package/docs/guides/workspace.mdx +181 -0
  40. package/docs/snippets/agent/architecture.mdx +72 -0
  41. package/docs/snippets/agent/bedrock-deployment.mdx +172 -0
  42. package/docs/snippets/agent/runtime-arn.mdx +64 -0
  43. package/docs/snippets/api/api-architecture.mdx +93 -0
  44. package/docs/snippets/api/api-choice-note.mdx +6 -0
  45. package/docs/snippets/api/cors-configuration-cdk-note.mdx +25 -0
  46. package/docs/snippets/api/cors-configuration-terraform-note.mdx +28 -0
  47. package/docs/snippets/api/shared-constructs.mdx +38 -0
  48. package/docs/snippets/api/type-safe-api-integrations.mdx +643 -0
  49. package/docs/snippets/api/waf-configuration.mdx +37 -0
  50. package/docs/snippets/connection/a2a-infrastructure.mdx +63 -0
  51. package/docs/snippets/connection/lambda-rdb-ssl-requirements.mdx +40 -0
  52. package/docs/snippets/connection/mcp-server-rdb-ssl-requirements.mdx +35 -0
  53. package/docs/snippets/connection/rdb-api-infrastructure.mdx +72 -0
  54. package/docs/snippets/connection/react-agent-infrastructure.mdx +61 -0
  55. package/docs/snippets/connection/strands-agent-rdb-ssl-requirements.mdx +35 -0
  56. package/docs/snippets/lambda-function/architecture.mdx +36 -0
  57. package/docs/snippets/lambda-function/deploying-your-function.mdx +118 -0
  58. package/docs/snippets/mcp/architecture.mdx +58 -0
  59. package/docs/snippets/mcp/assistant-docs.mdx +10 -0
  60. package/docs/snippets/mcp/bedrock-deployment.mdx +167 -0
  61. package/docs/snippets/mcp/config.mdx +13 -0
  62. package/docs/snippets/mcp/configuration-py.mdx +42 -0
  63. package/docs/snippets/mcp/configuration-ts.mdx +53 -0
  64. package/docs/snippets/mcp/observability.mdx +8 -0
  65. package/docs/snippets/mcp/shared-constructs.mdx +32 -0
  66. package/docs/snippets/pdk-migration/example/01-migrate-api.mdx +602 -0
  67. package/docs/snippets/pdk-migration/example/02-migrate-website.mdx +915 -0
  68. package/docs/snippets/pdk-migration/example/03-migrate-infra.mdx +161 -0
  69. package/docs/snippets/pdk-migration/example/04-deploy.mdx +229 -0
  70. package/docs/snippets/pdk-migration/faq/aws-arch.mdx +17 -0
  71. package/docs/snippets/pdk-migration/faq/cdk-graph.mdx +29 -0
  72. package/docs/snippets/pdk-migration/faq/infrastructure-python-java.mdx +19 -0
  73. package/docs/snippets/pdk-migration/faq/pdk-nag.mdx +15 -0
  74. package/docs/snippets/pdk-migration/faq/pipeline.mdx +15 -0
  75. package/docs/snippets/pdk-migration/faq/type-safe-api.mdx +310 -0
  76. package/docs/snippets/pdk-migration/faq/use-of-projen.mdx +15 -0
  77. package/docs/snippets/prerequisites.mdx +20 -0
  78. package/docs/snippets/required-prerequisites.mdx +12 -0
  79. package/docs/snippets/shared-constructs.mdx +40 -0
  80. package/docs/snippets/tools/acurl.mdx +73 -0
  81. package/docs/snippets/ts-bundle.mdx +14 -0
  82. package/package.json +1 -1
  83. package/src/py/agent/schema.json +1 -1
  84. package/src/py/fast-api/schema.json +2 -2
  85. package/src/py/mcp-server/schema.json +1 -1
  86. package/src/smithy/ts/api/schema.json +2 -2
  87. package/src/trpc/backend/schema.json +2 -2
  88. package/src/ts/agent/schema.json +1 -1
  89. package/src/ts/mcp-server/schema.json +1 -1
@@ -0,0 +1,483 @@
1
+ ---
2
+ title: Python Agent
3
+ description: Generate a Python Agent for building AI agents with tools and deploy to Amazon Bedrock AgentCore Runtime
4
+ generator: py#agent
5
+ ---
6
+
7
+ import { FileTree, Tabs, TabItem } from '@astrojs/starlight/components';
8
+ import RunGenerator from '@components/run-generator.astro';
9
+ import NxCommands from '@components/nx-commands.astro';
10
+ import Link from '@components/link.astro';
11
+ import Snippet from '@components/snippet.astro';
12
+ import Infrastructure from '@components/infrastructure.astro';
13
+ import GeneratorParameters from '@components/generator-parameters.astro';
14
+ import Drawer from '@components/drawer.astro';
15
+ import OptionFilter from '@components/option-filter.astro';
16
+
17
+ Generate a Python [Strands Agent](https://strandsagents.com/) for building AI agents with tools, and optionally deploy it to [Amazon Bedrock AgentCore Runtime](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/). By default, the generator uses [FastAPI](https://fastapi.tiangolo.com/) to expose an HTTP server. Alternatively, you can choose the [Agent-to-Agent (A2A)](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-a2a.html) protocol for interoperability with other A2A-compatible agents, or the [AG-UI](https://docs.ag-ui.com/) protocol for direct frontend integration via [CopilotKit](https://docs.copilotkit.ai/aws-strands).
18
+
19
+ ## What is Strands?
20
+
21
+ [Strands](https://strandsagents.com/docs/user-guide/quickstart/overview/) is a lightweight, production-ready Python framework for building AI agents. Key features include:
22
+
23
+ - **Lightweight and customizable**: Simple agent loop that gets out of your way
24
+ - **Production ready**: Full observability, tracing, and deployment options for scale
25
+ - **Model and provider agnostic**: Supports many different models from various providers
26
+ - **Community-driven tools**: Powerful set of community-contributed tools
27
+ - **Multi-agent support**: Advanced techniques like agent teams and autonomous agents
28
+ - **Flexible interaction modes**: Conversational, streaming, and non-streaming support
29
+
30
+ ## Usage
31
+
32
+ ### Generate an Agent
33
+
34
+ You can generate a Python Agent in two ways:
35
+
36
+ <RunGenerator generator="py#agent" />
37
+
38
+ :::tip[Project Prerequisite]
39
+ First use the <Link path="/guides/python-project">`py#project`</Link> generator to create a project to add your Agent to.
40
+ :::
41
+
42
+ ### Options
43
+
44
+ <GeneratorParameters generator="py#agent" />
45
+
46
+ ## Generator Output
47
+
48
+ The generator will add the following files to your existing Python project. The files generated depend on the chosen `protocol`:
49
+
50
+ <OptionFilter when={{ protocol: 'HTTP' }} description="FastAPI HTTP server layout">
51
+ ### HTTP Protocol (default)
52
+
53
+ <FileTree>
54
+ - your-project/
55
+ - your_module/
56
+ - agent/ (or custom name if specified)
57
+ - \_\_init\_\_.py Python package initialization
58
+ - init.py FastAPI application setup with CORS and error handling middleware
59
+ - agent.py Main agent definition with sample tools
60
+ - main.py FastAPI entry point for Bedrock AgentCore Runtime
61
+ - Dockerfile Entry point for hosting your agent (excluded when `computeType` is set to `None`)
62
+ - pyproject.toml Updated with Strands dependencies
63
+ - project.json Updated with agent serve targets
64
+ </FileTree>
65
+ </OptionFilter>
66
+
67
+ <OptionFilter when={{ protocol: 'A2A' }} description="Strands A2A server layout">
68
+ ### A2A Protocol
69
+
70
+ The entry point uses the [Strands A2A Server](https://strandsagents.com/docs/user-guide/concepts/multi-agent/agent-to-agent) instead of FastAPI:
71
+
72
+ <FileTree>
73
+ - your-project/
74
+ - your_module/
75
+ - agent/ (or custom name if specified)
76
+ - \_\_init\_\_.py Python package initialization
77
+ - agent.py Main agent definition with sample tools
78
+ - main.py A2A server entry point
79
+ - Dockerfile Entry point for hosting your agent (excluded when `computeType` is set to `None`)
80
+ - pyproject.toml Updated with Strands dependencies
81
+ - project.json Updated with agent serve targets
82
+ </FileTree>
83
+ </OptionFilter>
84
+
85
+ <OptionFilter when={{ protocol: 'AG-UI' }} description="AG-UI / CopilotKit server layout">
86
+ ### AG-UI Protocol
87
+
88
+ The entry point uses the [ag-ui-strands](https://docs.ag-ui.com/) integration, which exposes your agent via the AG-UI protocol for direct frontend integration with [CopilotKit](https://docs.copilotkit.ai/aws-strands):
89
+
90
+ <FileTree>
91
+ - your-project/
92
+ - your_module/
93
+ - agent/ (or custom name if specified)
94
+ - \_\_init\_\_.py Python package initialization
95
+ - agent.py Main agent definition with sample tools
96
+ - main.py AG-UI server entry point using ag-ui-strands
97
+ - Dockerfile Entry point for hosting your agent (excluded when `computeType` is set to `None`)
98
+ - pyproject.toml Updated with Strands and AG-UI dependencies
99
+ - project.json Updated with agent serve targets
100
+ </FileTree>
101
+
102
+ :::tip[Connect to a React website]
103
+ AG-UI agents can be connected to a React frontend using the <Link path="/guides/connection/react-agui">`connection` generator</Link>, which will set up [CopilotKit](https://docs.copilotkit.ai/aws-strands) components for a rich chat experience.
104
+ :::
105
+ </OptionFilter>
106
+
107
+ ### Infrastructure
108
+
109
+ <OptionFilter when={{ computeType: 'BedrockAgentCoreRuntime' }} description="Bedrock AgentCore Runtime deployment">
110
+ <Snippet name="shared-constructs" />
111
+
112
+ For deploying your Agent, the following files are generated:
113
+
114
+ <Infrastructure>
115
+ <Fragment slot="cdk">
116
+ <FileTree>
117
+ - packages/common/constructs/src
118
+ - app
119
+ - agents
120
+ - \<project-name>
121
+ - \<project-name>.ts CDK construct for deploying your agent
122
+ </FileTree>
123
+ </Fragment>
124
+ <Fragment slot="terraform">
125
+ <FileTree>
126
+ - packages/common/terraform/src
127
+ - app
128
+ - agents
129
+ - \<project-name>
130
+ - \<project-name>.tf Module for deploying your agent
131
+ - core
132
+ - agent-core
133
+ - runtime.tf Generic module for deploying to Bedrock AgentCore Runtime
134
+ </FileTree>
135
+ </Fragment>
136
+ </Infrastructure>
137
+ </OptionFilter>
138
+
139
+ <OptionFilter when={{ computeType: 'None' }} description="No infrastructure is vended for computeType=None">
140
+ If you selected `None` for `computeType`, no CDK constructs or Terraform modules are generated — the Agent can only be run locally. The `auth` option is ignored in this mode since there is no hosted endpoint to authenticate.
141
+ </OptionFilter>
142
+
143
+ #### Architecture
144
+
145
+ <Snippet name="agent/architecture" />
146
+
147
+ ## Working with Your Agent
148
+
149
+ ### Adding Tools
150
+
151
+ Tools are functions that the AI agent can call to perform actions. The Strands framework uses a simple decorator-based approach for defining tools.
152
+
153
+ You can add new tools in the `agent.py` file:
154
+
155
+ ```python
156
+ from strands import Agent, tool
157
+
158
+ @tool
159
+ def calculate_sum(numbers: list[int]) -> int:
160
+ """Calculate the sum of a list of numbers"""
161
+ return sum(numbers)
162
+
163
+ @tool
164
+ def get_weather(city: str) -> str:
165
+ """Get weather information for a city"""
166
+ # Your weather API integration here
167
+ return f"Weather in {city}: Sunny, 25°C"
168
+
169
+ # Add tools to your agent
170
+ agent = Agent(
171
+ system_prompt="You are a helpful assistant with access to various tools.",
172
+ tools=[calculate_sum, get_weather],
173
+ )
174
+ ```
175
+
176
+ The Strands framework automatically handles:
177
+ - Type validation based on your function's type hints
178
+ - JSON schema generation for tool calling
179
+ - Error handling and response formatting
180
+
181
+ ### Using Pre-built Tools
182
+
183
+ Strands provides a collection of pre-built tools through the `strands-tools` package:
184
+
185
+ ```python
186
+ from strands_tools import current_time, http_request, file_read
187
+
188
+ agent = Agent(
189
+ system_prompt="You are a helpful assistant.",
190
+ tools=[current_time, http_request, file_read],
191
+ )
192
+ ```
193
+
194
+ ### Model Configuration
195
+
196
+ By default, Strands agents use Claude 4 Sonnet, but you can customize the model provider. See the [Strands documentation on model providers](https://strandsagents.com/docs/user-guide/concepts/model-providers/) for configuration options:
197
+
198
+ ```python
199
+ from strands import Agent
200
+ from strands.models import BedrockModel
201
+
202
+ # Create a BedrockModel
203
+ bedrock_model = BedrockModel(
204
+ model_id="anthropic.claude-sonnet-4-20250514-v1:0",
205
+ region_name="us-west-2",
206
+ temperature=0.3,
207
+ )
208
+
209
+ agent = Agent(model=bedrock_model)
210
+ ```
211
+
212
+ ### Consuming MCP Servers
213
+
214
+ You can [add tools from MCP servers](https://strandsagents.com/docs/user-guide/concepts/tools/mcp-tools/) to your Strands agent.
215
+
216
+ For consuming MCP Servers which you have created using the <Link path="/guides/py-mcp-server">`py#mcp-server`</Link> or <Link path="/guides/ts-mcp-server">`ts#mcp-server`</Link> generators you can make use of the <Link path="/guides/connection/py-agent-mcp">`connection` generator</Link>.
217
+
218
+ <RunGenerator generator="connection" />
219
+
220
+ Refer to the <Link path="/guides/connection/py-agent-mcp">`connection` generator guide</Link> for details about how the connection is set up.
221
+
222
+ For other MCP servers, please refer to the [Strands Documentation](https://strandsagents.com/docs/user-guide/concepts/tools/mcp-tools/).
223
+
224
+ ### More
225
+
226
+ For a more in-depth guide to writing Strands agents, refer to the [Strands documentation](https://strandsagents.com/docs/user-guide/quickstart/overview/).
227
+
228
+ ## Protocol
229
+
230
+ Your agent's server protocol determines how it communicates. All options are served by [FastAPI](https://fastapi.tiangolo.com/) — the entry point differs:
231
+
232
+ - **HTTP** (default): A standard FastAPI server with a custom `/invocations` endpoint, CORS, and streaming. Best for custom client integrations.
233
+ - **A2A**: The [Strands A2A Server](https://strandsagents.com/docs/user-guide/concepts/multi-agent/agent-to-agent) mounted onto a FastAPI app. Best when your agent needs to be discoverable and invokable by other A2A-compatible agents.
234
+ - **AG-UI**: The [ag-ui-strands](https://docs.ag-ui.com/) integration, which exposes the [AG-UI protocol](https://docs.ag-ui.com/) over SSE. Best for direct frontend integration with [CopilotKit](https://docs.copilotkit.ai/aws-strands) in a React website.
235
+
236
+ All protocols expose `/ping` for the AgentCore runtime health check contract. A2A agents listen on port `9000`; HTTP and AG-UI agents listen on port `8080`. The generated Dockerfile and infrastructure are configured for you.
237
+
238
+ <OptionFilter when={{ protocol: 'HTTP' }} description="FastAPI HTTP server details">
239
+ ## FastAPI Server (HTTP protocol)
240
+
241
+ The generated HTTP server includes:
242
+ - FastAPI application setup with CORS middleware
243
+ - Error handling middleware
244
+ - OpenAPI schema generation
245
+ - Health check endpoint (`/ping`)
246
+ - Agent invocation endpoint (`/invocations`)
247
+
248
+ ### Customizing Invoke Inputs and Outputs with Pydantic
249
+
250
+ The agent's invocation endpoint uses [Pydantic](https://docs.pydantic.dev/) models to define and validate the request and response schemas. You can customize these models in `main.py` to match your agent's requirements.
251
+
252
+ #### Defining Input Models
253
+
254
+ The default `InvokeInput` model accepts a message.
255
+
256
+ ```python
257
+ from pydantic import BaseModel
258
+
259
+ class InvokeInput(BaseModel):
260
+ message: str
261
+ ```
262
+
263
+ You can extend this model to include any additional fields your agent needs.
264
+
265
+ The session ID is extracted from the `x-amzn-bedrock-agentcore-runtime-session-id` HTTP header, consistent with the [Bedrock AgentCore Runtime session contract](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-service-contract.html). If the header is not provided, a random UUID is generated as a fallback.
266
+
267
+ :::caution[Code Organization]
268
+ You will likely want to abstract some or all of the session ID from the caller if users are to invoke your agent directly. For example, you may use the authenticated user ID as part of the session ID.
269
+
270
+ It is also worth noting that depending on your use case you may need to implement authorization for sessions, for example ensuring that users may only access their own sessions and not those of other users.
271
+ :::
272
+
273
+ #### Defining Output Models
274
+
275
+ For streaming responses, the generator provides `JsonStreamingResponse` which automatically serializes Pydantic models to JSON Lines format (`application/jsonl`). This format is compatible with OpenAPI 3.2's streaming specification and works seamlessly with the generated TypeScript client.
276
+
277
+ By default, the agent yields `StreamChunk` objects containing the agent's response text:
278
+
279
+ ```python
280
+ class StreamChunk(BaseModel):
281
+ content: str
282
+ ```
283
+
284
+ You can customise the `StreamChunk` model to suit your needs:
285
+
286
+ ```python
287
+ from pydantic import BaseModel
288
+
289
+ class StreamChunk(BaseModel):
290
+ content: str
291
+ timestamp: str
292
+ token_count: int
293
+ ```
294
+
295
+ :::note[FastAPI Streaming Limitation]
296
+ Note that since FastAPI does not currently support generating an OpenAPI specification for streaming responses, we must explicitly set the response model for each streaming operation, ie:
297
+
298
+ ```python
299
+ @app.post(
300
+ "/invocations",
301
+ response_class=JsonStreamingResponse,
302
+ responses={200: JsonStreamingResponse.openapi_response(StreamChunk)},
303
+ )
304
+ async def invoke(input: InvokeInput) -> JsonStreamingResponse:
305
+ return JsonStreamingResponse(handle_invoke(input))
306
+ ```
307
+ :::
308
+
309
+ There is an [open feature request for native support in FastAPI](https://github.com/fastapi/fastapi/discussions/14362).
310
+ </OptionFilter>
311
+
312
+ ## Bedrock AgentCore Python SDK
313
+
314
+ The generator includes a dependency on the [Bedrock AgentCore Python SDK](https://github.com/aws/bedrock-agentcore-sdk-python) for the `PingStatus` constants. If desired, it is straightforward to use `BedrockAgentCoreApp` instead of FastAPI, however note that type-safety is lost.
315
+
316
+ You can find more details about the SDK's capabilities in the [documentation here](https://aws.github.io/bedrock-agentcore-starter-toolkit/user-guide/runtime/quickstart.html).
317
+
318
+ :::note[Infrastructure as Code]
319
+ Since the generator vends CDK or Terraform infrastructure which manages deploying your agent, you do not need to utilise the `bedrock-agentcore-starter-toolkit` which the docs mention for deploying your agent.
320
+ :::
321
+
322
+ <OptionFilter when={{ protocol: 'A2A' }} description="A2A server details">
323
+ ## A2A Server (A2A protocol)
324
+
325
+ The generated `main.py` mounts `A2AServer.to_fastapi_app()` onto a parent FastAPI app that also exposes `/ping`. When deployed to AgentCore, the entry point resolves the runtime's public ARN from AppConfig and advertises it in the agent card.
326
+
327
+ Most users will not need to modify this file — edit `agent.py` to change tools or the system prompt. The A2A server populates the agent card (`/.well-known/agent-card.json`) from the `Agent` constructor's `name` and `description`.
328
+ </OptionFilter>
329
+
330
+ <OptionFilter when={{ protocol: 'AG-UI' }} description="AG-UI / CopilotKit server details">
331
+ ## AG-UI Server (AG-UI protocol)
332
+
333
+ The generated `main.py` wraps your Strands `Agent` in an `ag_ui_strands.StrandsAgent` and creates a FastAPI app via `create_strands_app()`. The resulting app exposes a single POST endpoint that streams [AG-UI](https://docs.ag-ui.com/) events over Server-Sent Events (SSE), as well as `/ping` for the AgentCore runtime health check.
334
+
335
+ Most users will not need to modify this file — edit `agent.py` to change tools or the system prompt.
336
+
337
+ :::tip[Connecting to a React website]
338
+ AG-UI agents are designed to be consumed directly by a frontend. Use the <Link path="/guides/connection/react-agui">`connection` generator</Link> to wire your React website up to the agent with a [CopilotKit](https://docs.copilotkit.ai/aws-strands) provider and [AG-UI HttpAgent](https://docs.ag-ui.com/) client.
339
+ :::
340
+ </OptionFilter>
341
+
342
+ ## Running Your Agent
343
+
344
+ ### Local Development
345
+
346
+ The generator configures a target named `<your-agent-name>-serve`, which starts your Agent locally for development and testing.
347
+
348
+ <NxCommands commands={['agent-serve your-project']} />
349
+
350
+ This command uses `uv run` to execute your Agent using the [Bedrock AgentCore Python SDK](https://github.com/aws/bedrock-agentcore-sdk-python).
351
+
352
+ ### Chat with Your Agent
353
+
354
+ The generator configures a `<your-agent-name>-chat` Nx target that depends on `<your-agent-name>-serve-local`. Running it starts the agent locally and drops you into an interactive terminal chat:
355
+
356
+ <NxCommands commands={['run your-project:agent-chat']} />
357
+
358
+ For **HTTP** agents, the generator also emits:
359
+
360
+ - `scripts/<your-agent-name>_openapi.py` — a small script that exports the agent's OpenAPI spec
361
+ - An `<your-agent-name>-openapi` Nx target that runs it
362
+ - An `<your-agent-name>-generate-client` Nx target that produces a type-safe TypeScript client under `scripts/<your-agent-name>/generated/`
363
+ - `scripts/<your-agent-name>/chat.ts` — a thin wrapper around the generated client
364
+
365
+ When you customize the agent's input shape (e.g. add new fields to `InvokeInput`), update `chat.ts` to pass the new fields when invoking the agent and the rest works automatically.
366
+
367
+ <OptionFilter when={{ computeType: 'BedrockAgentCoreRuntime' }} description="Bedrock AgentCore Runtime deployment details">
368
+ ## Deploying Your Agent to Bedrock AgentCore Runtime
369
+
370
+ <Snippet name="agent/bedrock-deployment" parentHeading="Deploying Your Agent to Bedrock AgentCore Runtime" />
371
+
372
+ ### Bundle and Docker Targets
373
+
374
+ In order to build your Agent for Bedrock AgentCore Runtime, a `bundle` target is added to your project, which:
375
+
376
+ - Exports your Python dependencies to a `requirements.txt` file using `uv export`
377
+ - Installs dependencies for the target platform (`aarch64-manylinux_2_28`) using `uv pip install`
378
+
379
+ A `docker` target specific to your Agent is also added, which copies the `Dockerfile` and bundled artifacts into a docker context directory. This co-locates the `Dockerfile` with the built output, allowing CDK to build the Docker image directly using `AgentRuntimeArtifact.fromAsset`.
380
+
381
+ ### Observability
382
+
383
+ Your agent is automatically configured with observability using the [AWS Distro for Open Telemetry](https://aws.amazon.com/otel/) (ADOT), by configuring auto-instrumentation in your `Dockerfile`.
384
+
385
+ You can find traces in the CloudWatch AWS Console, by selecting "GenAI Observability" in the menu. Note that for traces to be populated you will need to enable [Transaction Search](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Transaction-Search.html).
386
+
387
+ For more details, refer to the [AgentCore documentation on observability](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/observability-configure.html).
388
+ </OptionFilter>
389
+
390
+ ## Invoking your Agent
391
+
392
+ <OptionFilter when={{ protocol: 'HTTP' }} description="FastAPI HTTP invocation details">
393
+ ### Invoke the Local Server
394
+
395
+ To invoke an Agent running locally via the `<your-agent-name>-serve` target, you can send a simple POST request to `/invocations` on the port your local agent is running on. For example, with `curl`:
396
+
397
+ ```bash
398
+ curl -N -X POST http://localhost:8081/invocations \
399
+ -d '{"message": "what is 3 + 5?"}' \
400
+ -H "Content-Type: application/json"
401
+ ```
402
+
403
+ :::note[Curl Streaming Flag]
404
+ The `-N` argument given to `curl` disables buffering the output stream, so you can see the streaming response in real-time.
405
+ :::
406
+
407
+ ### Invoke the Deployed Agent
408
+
409
+ <Snippet name="agent/runtime-arn" parentHeading="Invoke the Deployed Agent" />
410
+
411
+ <Tabs syncKey="auth">
412
+ <TabItem label="IAM" _filter={{ auth: 'IAM' }}>
413
+ #### IAM Authentication
414
+
415
+ For IAM Authentication, the request must be signed using AWS Signature Version 4 (SigV4).
416
+
417
+ ```bash
418
+ acurl <region> bedrock-agentcore -N -X POST \
419
+ 'https://bedrock-agentcore.<region>.amazonaws.com/runtimes/<url-encoded-arn>/invocations' \
420
+ -d '{"message": "what is 3 + 5?"}' \
421
+ -H 'Content-Type: application/json'
422
+ ```
423
+
424
+ <Drawer title="Sigv4 enabled curl" trigger="Click here for more details on configuring the above acurl command">
425
+ <Snippet name="tools/acurl" />
426
+ </Drawer>
427
+ </TabItem>
428
+
429
+ <TabItem label="Cognito" _filter={{ auth: 'Cognito' }}>
430
+ #### JWT / Cognito Authentication
431
+
432
+ For Cognito Authentication, pass the Cognito Access Token in the `Authorization` header:
433
+
434
+ ```bash
435
+ curl -N -X POST 'https://bedrock-agentcore.<region>.amazonaws.com/runtimes/<url-encoded-arn>/invocations' \
436
+ -d '{"message": "what is 3 + 5?"}' \
437
+ -H "Content-Type: application/json" \
438
+ -H "Authorization: Bearer <access-token>"
439
+ ```
440
+
441
+ You can obtain the access token using the AWS CLI's `cognito-idp admin-initiate-auth` command, for example:
442
+
443
+ ```bash
444
+ aws cognito-idp admin-initiate-auth \
445
+ --user-pool-id <user-pool-id> \
446
+ --client-id <user-pool-client-id> \
447
+ --auth-flow ADMIN_NO_SRP_AUTH \
448
+ --auth-parameters USERNAME=<username>,PASSWORD=<password> \
449
+ --region <region> \
450
+ --query 'AuthenticationResult.AccessToken' \
451
+ --output text
452
+ ```
453
+ </TabItem>
454
+ </Tabs>
455
+
456
+ #### Browser / React Website
457
+
458
+ For invoking your Agent from a React website, you can make use of the <Link path="/guides/connection/react-py-agent">`connection` generator</Link>, which automatically sets up a client with the correct authentication (IAM or Cognito).
459
+
460
+ <RunGenerator generator="connection" />
461
+
462
+ Refer to the <Link path="/guides/connection/react-py-agent">`connection` generator guide</Link> for details about how the connection is set up.
463
+ </OptionFilter>
464
+
465
+ <OptionFilter when={{ protocol: 'A2A' }} description="A2A delegation details">
466
+ ### Invoking an A2A Agent as a Tool
467
+
468
+ To delegate work from this agent to a remote A2A agent (either <Link path="/guides/ts-agent">TypeScript</Link> or <Link path="/guides/py-agent">Python</Link>), use the <Link path="/guides/connection/py-agent-a2a">`connection` generator</Link>. It vends a SigV4-authenticated client for the target agent and AST-transforms this agent's `agent.py` to register the remote A2A agent as a `@tool`-decorated delegate.
469
+
470
+ <RunGenerator generator="connection" />
471
+
472
+ Refer to the <Link path="/guides/connection/py-agent-a2a">`connection` generator guide</Link> for details about how the connection is set up.
473
+ </OptionFilter>
474
+
475
+ <OptionFilter when={{ protocol: 'AG-UI' }} description="AG-UI / React connection details">
476
+ ### Invoking an AG-UI Agent
477
+
478
+ To invoke your AG-UI agent from a React website, use the <Link path="/guides/connection/react-agui">`connection` generator</Link>, which wires up a [CopilotKit](https://docs.copilotkit.ai/aws-strands) client configured for your deployed agent with the correct authentication (IAM or Cognito).
479
+
480
+ <RunGenerator generator="connection" />
481
+
482
+ Refer to the <Link path="/guides/connection/react-agui">`connection` generator guide</Link> for details about how the connection is set up.
483
+ </OptionFilter>
@@ -0,0 +1,161 @@
1
+ ---
2
+ title: Python MCP Server
3
+ description: Generate a Python Model Context Protocol (MCP) server for providing context to Large Language Models
4
+ generator: py#mcp-server
5
+ ---
6
+
7
+ import { FileTree } from '@astrojs/starlight/components';
8
+ import RunGenerator from '@components/run-generator.astro';
9
+ import NxCommands from '@components/nx-commands.astro';
10
+ import Link from '@components/link.astro';
11
+ import Snippet from '@components/snippet.astro';
12
+ import GeneratorParameters from '@components/generator-parameters.astro';
13
+ import OptionFilter from '@components/option-filter.astro';
14
+
15
+ Generate a Python [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server for providing context to Large Language Models (LLMs), and optionally deploy it to [Amazon Bedrock AgentCore](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/).
16
+
17
+ ## What is MCP?
18
+
19
+ The [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) is an open standard that allows AI assistants to interact with external tools and resources. It provides a consistent way for LLMs to:
20
+
21
+ - Execute tools (functions) that perform actions or retrieve information
22
+ - Access resources that provide context or data
23
+
24
+ ## Usage
25
+
26
+ ### Generate an MCP Server
27
+
28
+ You can generate a Python MCP server in two ways:
29
+
30
+ <RunGenerator generator="py#mcp-server" />
31
+
32
+ :::tip[Project Prerequisite]
33
+ First use the <Link path="/guides/python-project">`py#project`</Link> generator to create a project to add your MCP server to.
34
+ :::
35
+
36
+ ### Options
37
+
38
+ <GeneratorParameters generator="py#mcp-server" />
39
+
40
+ ## Generator Output
41
+
42
+ The generator will add the following files to your existing Python project:
43
+
44
+ <FileTree>
45
+ - your-project/
46
+ - your_module/
47
+ - mcp_server/ (or custom name if specified)
48
+ - \_\_init\_\_.py Python package initialization
49
+ - server.py Main server definition with sample tools and resources
50
+ - stdio.py Entry point for STDIO transport, useful for simple local MCP servers
51
+ - http.py Entry point for Streamable HTTP transport, useful for hosting your MCP server
52
+ - Dockerfile Entry point for hosting your MCP server (excluded when `computeType` is set to `None`)
53
+ - pyproject.toml Updated with MCP dependencies
54
+ - project.json Updated with MCP server serve targets
55
+ </FileTree>
56
+
57
+ ### Infrastructure
58
+
59
+ <OptionFilter when={{ computeType: 'BedrockAgentCoreRuntime' }} description="Bedrock AgentCore Runtime deployment">
60
+ <Snippet name="shared-constructs" />
61
+
62
+ <Snippet name="mcp/shared-constructs" />
63
+ </OptionFilter>
64
+
65
+ <OptionFilter when={{ computeType: 'None' }} description="No infrastructure is vended for computeType=None">
66
+ If you selected `None` for `computeType`, no CDK constructs or Terraform modules are generated — the MCP server is configured for local STDIO / HTTP use only. The `auth` option is ignored in this mode since there is no hosted endpoint to authenticate.
67
+ </OptionFilter>
68
+
69
+ #### Architecture
70
+
71
+ <Snippet name="mcp/architecture" />
72
+
73
+ ## Working with Your MCP Server
74
+
75
+ ### Adding Tools
76
+
77
+ Tools are functions that the AI assistant can call to perform actions. The Python MCP server uses the [MCP Python SDK (FastMCP)](https://github.com/modelcontextprotocol/python-sdk) library, which provides a simple decorator-based approach for defining tools.
78
+
79
+ You can add new tools in the `server.py` file:
80
+
81
+ ```python
82
+ @mcp.tool(description="Your tool description")
83
+ def your_tool_name(param1: str, param2: int) -> str:
84
+ """Tool implementation with type hints"""
85
+ # Your tool logic here
86
+ return f"Result: {param1} with {param2}"
87
+ ```
88
+
89
+ The FastMCP library automatically handles:
90
+ - Type validation based on your function's type hints
91
+ - JSON schema generation for the MCP protocol
92
+ - Error handling and response formatting
93
+
94
+ ### Adding Resources
95
+
96
+ Resources provide context to the AI assistant. You can add resources using the `@mcp.resource` decorator:
97
+
98
+ ```python
99
+ @mcp.resource("example://static-resource", description="Static resource example")
100
+ def static_resource() -> str:
101
+ """Return static content"""
102
+ return "This is static content that provides context to the AI"
103
+
104
+ @mcp.resource("dynamic://resource/{item_id}", description="Dynamic resource example")
105
+ def dynamic_resource(item_id: str) -> str:
106
+ """Return dynamic content based on parameters"""
107
+ # Fetch data based on item_id
108
+ data = fetch_data_for_item(item_id)
109
+ return f"Dynamic content for {item_id}: {data}"
110
+ ```
111
+
112
+ ## Configuring with AI Assistants
113
+
114
+ <Snippet name="mcp/configuration-py" parentHeading="Configuring with AI Assistants" />
115
+
116
+ ## Running Your MCP Server
117
+
118
+ ### Inspector
119
+
120
+ The generator configures a target named `<your-server-name>-inspect`, which starts the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) with the configuration to connect to your MCP server using STDIO transport.
121
+
122
+ <NxCommands commands={['your-server-name-inspect your-project']} />
123
+
124
+ This will start the inspector at `http://localhost:6274`. Get started by clicking on the "Connect" button.
125
+
126
+ ### STDIO
127
+
128
+ The easiest way to test and use an MCP server is by using the inspector or configuring it with an AI assistant (as above).
129
+
130
+ You can however run your server with [STDIO transport](https://modelcontextprotocol.io/docs/concepts/transports#standard-input%2Foutput-stdio) directly using the `<your-server-name>-serve-stdio` target.
131
+
132
+ <NxCommands commands={['your-server-name-serve-stdio your-project']} />
133
+
134
+ This command uses `uv run` to execute your MCP server with STDIO transport.
135
+
136
+ ### Streamable HTTP
137
+
138
+ If you would like to run your MCP server locally using [Streamable HTTP transport](https://modelcontextprotocol.io/docs/concepts/transports#streamable-http), you can use the `<your-server-name>-serve` target.
139
+
140
+ <NxCommands commands={['your-server-name-serve your-project']} />
141
+
142
+ This command uses `uv run uvicorn --reload` to run your MCP server with HTTP transport (typically on port `8000`), and automatically restarts when files change.
143
+
144
+ <OptionFilter when={{ computeType: 'BedrockAgentCoreRuntime' }} description="Bedrock AgentCore Runtime deployment details">
145
+ ## Deploying Your MCP Server to Bedrock AgentCore Runtime
146
+
147
+ <Snippet name="mcp/bedrock-deployment" parentHeading="Deploying Your MCP Server to Bedrock AgentCore Runtime" />
148
+
149
+ ### Bundle and Docker Targets
150
+
151
+ In order to build your MCP server for Bedrock AgentCore Runtime, a `bundle` target is added to your project, which:
152
+
153
+ - Exports your Python dependencies to a `requirements.txt` file using `uv export`
154
+ - Installs dependencies for the target platform (`aarch64-manylinux_2_28`) using `uv pip install`
155
+
156
+ A `docker` target specific to your MCP server is also added, which copies the `Dockerfile` and bundled artifacts into a docker context directory. This co-locates the `Dockerfile` with the built output, allowing CDK to build the Docker image directly using `AgentRuntimeArtifact.fromAsset`.
157
+
158
+ ### Observability
159
+
160
+ <Snippet name="mcp/observability" parentHeading="Observability" />
161
+ </OptionFilter>