@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
package/bin/aws-nx-mcp.js CHANGED
@@ -19408,10 +19408,10 @@ const buildInstallCommand = (pm, pkg, dev) => {
19408
19408
  * The package manager is auto-detected by @aws/create-nx-workspace from the
19409
19409
  * invoking command, so --pm is not needed.
19410
19410
  */
19411
- const buildCreateNxWorkspaceCommand = (pm, workspace, iacProvider) => {
19411
+ const buildCreateNxWorkspaceCommand = (pm, workspace, iacProvider, tag) => {
19412
19412
  return [
19413
19413
  PACKAGE_MANAGER_COMMANDS[pm]?.create ?? `${pm} create`,
19414
- "@aws/nx-workspace",
19414
+ tag ? `@aws/nx-workspace@${tag}` : "@aws/nx-workspace",
19415
19415
  ...pm === "npm" ? ["--"] : [],
19416
19416
  workspace,
19417
19417
  ...iacProvider ? [`--iacProvider=${iacProvider}`] : []
@@ -52676,12 +52676,12 @@ const fetchGuideFrontmatters = async (info) => {
52676
52676
  }));
52677
52677
  };
52678
52678
  /**
52679
- * Read a guide's raw MDX, local filesystem first, GitHub fallback.
52679
+ * Read a guide's raw MDX from the local filesystem (bundled or monorepo).
52680
52680
  */
52681
52681
  const fetchGuideRaw = async (page) => {
52682
52682
  const local = fetchLocalGuide(page);
52683
52683
  if (local !== void 0) return local;
52684
- return await (await fetch(`https://raw.githubusercontent.com/awslabs/nx-plugin-for-aws/refs/heads/main/docs/src/content/docs/en/guides/${page}.mdx`)).text();
52684
+ return "";
52685
52685
  };
52686
52686
  /**
52687
52687
  * Parse a guide's MDX once and return the body, the frontmatter, and the
@@ -52707,17 +52707,16 @@ const parseGuide = async (raw) => {
52707
52707
  };
52708
52708
  };
52709
52709
  /**
52710
- * Search the monorepo-relative guides directory for a page before falling
52711
- * back to GitHub. This is the common case during local development
52712
- * (`pnpm nx mcp-inspect @aws/nx-plugin`), and it also means test workspaces
52713
- * that link the built plugin read the guides they're actually iterating on
52714
- * instead of whatever is currently on `main`.
52715
- *
52716
- * When running as the published `@aws/nx-plugin-mcp` package, `__dirname`
52717
- * resolves somewhere inside `node_modules/@aws/nx-plugin-mcp`, the local
52718
- * probe misses, and we fall back to GitHub.
52710
+ * Probe paths for finding guide MDX files locally. Checked in order:
52711
+ * 1. Bundled docs in the published @aws/nx-plugin-mcp package
52712
+ * 2. Source checkout when running from the monorepo (dev/test)
52713
+ * 3. Rolldown-bundled binary in dist/ (monorepo layout)
52719
52714
  */
52720
- const GUIDES_RELATIVE_PROBES = ["../../../docs/src/content/docs/en/guides", "../../../../docs/src/content/docs/en/guides"];
52715
+ const GUIDES_RELATIVE_PROBES = [
52716
+ "../docs/guides",
52717
+ "../../../docs/src/content/docs/en/guides",
52718
+ "../../../../docs/src/content/docs/en/guides"
52719
+ ];
52721
52720
  const fetchLocalGuide = (guide) => {
52722
52721
  for (const rel of GUIDES_RELATIVE_PROBES) {
52723
52722
  const candidate = path.default.resolve(__dirname, rel, `${guide}.mdx`);
@@ -52727,24 +52726,19 @@ const fetchLocalGuide = (guide) => {
52727
52726
  }
52728
52727
  };
52729
52728
  /**
52730
- * Fetch markdown guide pages. Prefers local files (see `fetchLocalGuide`)
52731
- * and falls back to fetching from the repo on `main` when no local copy
52732
- * is available.
52729
+ * Fetch markdown guide pages from the local filesystem (bundled or monorepo).
52733
52730
  */
52734
52731
  const fetchGuidePages = async (guidePages, generators, packageManager, snippetContentProvider, options) => {
52735
- const fulfilled = (await Promise.allSettled(guidePages.map(async (guide) => {
52736
- const local = fetchLocalGuide(guide);
52737
- if (local !== void 0) return local;
52738
- return await (await fetch(`https://raw.githubusercontent.com/awslabs/nx-plugin-for-aws/refs/heads/main/docs/src/content/docs/en/guides/${guide}.mdx`)).text();
52739
- }))).filter((result) => result.status === "fulfilled");
52740
- return (await Promise.all(fulfilled.map((result) => postProcessGuide(result.value, generators, packageManager, snippetContentProvider, options)))).join("\n\n");
52732
+ const guides = guidePages.map((guide) => fetchLocalGuide(guide)).filter((content) => content !== void 0);
52733
+ return (await Promise.all(guides.map((content) => postProcessGuide(content, generators, packageManager, snippetContentProvider, options)))).join("\n\n");
52741
52734
  };
52742
- const SNIPPETS_RELATIVE_PROBES = ["../../../docs/src/content/docs/en/snippets", "../../../../docs/src/content/docs/en/snippets"];
52743
- const SNIPPET_BASE_URL = "https://raw.githubusercontent.com/awslabs/nx-plugin-for-aws/refs/heads/main/docs/src/content/docs/en/snippets";
52735
+ const SNIPPETS_RELATIVE_PROBES = [
52736
+ "../docs/snippets",
52737
+ "../../../docs/src/content/docs/en/snippets",
52738
+ "../../../../docs/src/content/docs/en/snippets"
52739
+ ];
52744
52740
  /**
52745
- * Fetch a snippet's content. Tries the local repo checkout first (when
52746
- * running under `mcp-inspect` or a linked test workspace) before falling
52747
- * back to the copy on `main`.
52741
+ * Fetch a snippet's content from the local filesystem (bundled or monorepo).
52748
52742
  */
52749
52743
  const fetchSnippet = async (snippetName) => {
52750
52744
  for (const rel of SNIPPETS_RELATIVE_PROBES) {
@@ -52753,13 +52747,7 @@ const fetchSnippet = async (snippetName) => {
52753
52747
  if (fs.default.existsSync(candidate)) return fs.default.readFileSync(candidate, "utf-8");
52754
52748
  } catch {}
52755
52749
  }
52756
- try {
52757
- const response = await fetch(`${SNIPPET_BASE_URL}/${snippetName}.mdx`);
52758
- if (!response.ok) return "";
52759
- return await response.text();
52760
- } catch {
52761
- return "";
52762
- }
52750
+ return "";
52763
52751
  };
52764
52752
  /**
52765
52753
  * Post-process a guide page. Thin wrapper around the unified/remark-mdx
@@ -0,0 +1,142 @@
1
+ ---
2
+ title: Astro Docs
3
+ description: Reference documentation for an Astro + Starlight documentation site
4
+ generator: ts#astro-docs
5
+ ---
6
+ import { FileTree } from '@astrojs/starlight/components';
7
+ import RunGenerator from '@components/run-generator.astro';
8
+ import GeneratorParameters from '@components/generator-parameters.astro';
9
+ import NxCommands from '@components/nx-commands.astro';
10
+
11
+ This generator scaffolds a documentation site powered by [Astro](https://astro.build/)
12
+ and the [Starlight](https://starlight.astro.build/) docs theme. It wires up
13
+ localisation, reusable content snippets, locale-aware internal links and a
14
+ [`starlight-blog`](https://starlight-blog-docs.vercel.app/) plugin by default.
15
+
16
+ By default it also scaffolds an automated translation pipeline powered by a
17
+ [Strands Agent](https://strandsagents.com/) on
18
+ [Amazon Bedrock](https://aws.amazon.com/bedrock/).
19
+
20
+ ## Usage
21
+
22
+ ### Generate an Astro docs site
23
+
24
+ You can generate a new Astro docs site in two ways:
25
+
26
+ <RunGenerator generator="ts#astro-docs" />
27
+
28
+ ### Options
29
+
30
+ <GeneratorParameters generator="ts#astro-docs" />
31
+
32
+ ## Generator Output
33
+
34
+ By default the generator creates the following project structure at `docs/` in the
35
+ workspace root (configurable via the `name`, `directory` and `subDirectory` options):
36
+
37
+ <FileTree>
38
+ - astro.config.mjs Astro + Starlight configuration (locales, sidebar, blog plugin)
39
+ - tsconfig.json Extends astro/tsconfigs/strict with @components / @assets path aliases
40
+ - project.json Nx project with `build`, `start`, `preview` (and `translate` if enabled) targets
41
+ - scripts
42
+ - translate.ts Translation driver — a Strands agent with a scoped file-editor tool (omitted with `--noTranslation`)
43
+ - translate.config.json Source/target locales, glob patterns, model id, region (omitted with `--noTranslation`)
44
+ - src
45
+ - components
46
+ - link.astro Locale-aware link component (resolves paths against the current locale)
47
+ - snippet.astro Locale-aware snippet loader component
48
+ - content
49
+ - docs
50
+ - en
51
+ - index.mdx Landing page
52
+ - guides
53
+ - getting-started.mdx Sample guide referencing the link and snippet components
54
+ - blog
55
+ - welcome.mdx Sample blog post (omitted with `--noBlog`)
56
+ - snippets
57
+ - example.mdx Sample reusable snippet
58
+ - styles
59
+ - custom.css Starlight theme overrides
60
+ - README.md Project README
61
+ </FileTree>
62
+
63
+ ### Localisation
64
+
65
+ The generator defaults to a single locale (`en`) and redirects the root URL to
66
+ it. To add more languages:
67
+
68
+ 1. Add an entry under `locales` in `astro.config.mjs` (for example `ko: { label: '한국어' }`).
69
+ 2. Create a matching directory under `src/content/docs/<locale>/`.
70
+ 3. Populate it manually, or use the translation target described below.
71
+
72
+ ## Translation
73
+
74
+ Unless you passed `--noTranslation`, the generator adds a `translate` target to
75
+ `project.json`, so you can run:
76
+
77
+ <NxCommands commands={[
78
+ 'translate docs -- --all',
79
+ 'translate docs -- --languages jp,ko',
80
+ 'translate docs -- --dry-run',
81
+ ]} />
82
+
83
+ When run without `--all`, the script only translates files that have changed
84
+ since the last translation commit on the current branch — meaning you can
85
+ safely re-run it on every docs PR without re-translating the whole site.
86
+
87
+ ### Configuring translation
88
+
89
+ Edit `scripts/translate.config.json` to change:
90
+
91
+ | Field | Purpose |
92
+ | --- | --- |
93
+ | `sourceLanguage` | Locale to translate **from** (default `en`). |
94
+ | `targetLanguages` | Locales to translate **to**. Empty by default. For example `["fr", "de", "es", "ja", "ko"]`. |
95
+ | `docsDir` | Path to the docs content directory, relative to the project root. |
96
+ | `include` | Glob patterns (relative to `<docsDir>/<sourceLanguage>`) for files to translate. |
97
+ | `exclude` | Glob patterns to skip. |
98
+ | `modelId` | Bedrock model to use for translations. |
99
+ | `awsRegion` | AWS region the Bedrock client is configured with. Can also be set via `AWS_REGION`. |
100
+ | `concurrency` | Max number of concurrent agent invocations. |
101
+ | `translationCommitMessage` | Commit message marker for translation commits (default `docs: update translations`). |
102
+
103
+ ### Locale-aware internal links
104
+
105
+ The generator ships a `Link` component that automatically resolves internal
106
+ docs paths against the current locale, so a single source of truth produces
107
+ the right URL in every language:
108
+
109
+ ```mdx
110
+ import Link from '@components/link.astro';
111
+
112
+ <Link path="guides/getting-started">Read the getting-started guide</Link>
113
+ ```
114
+
115
+ ### Snippets
116
+
117
+ Reusable content fragments live in `src/content/docs/<locale>/snippets/`. The
118
+ generated `Snippet` component loads the snippet that matches the current
119
+ locale:
120
+
121
+ ```mdx
122
+ import Snippet from '@components/snippet.astro';
123
+
124
+ <Snippet name="example" />
125
+ ```
126
+
127
+ ### Wiring up CI
128
+
129
+ No CI workflow is generated out of the box — add one that:
130
+
131
+ 1. Configures AWS credentials with permission to invoke Bedrock `InvokeModel` on
132
+ the configured model.
133
+ 2. Runs the `translate` target on pull requests that touch your source-language
134
+ docs:
135
+
136
+ <NxCommands commands={['translate docs']} />
137
+
138
+ 3. Commits the resulting translations back to the PR branch. The commit message
139
+ must match the `translationCommitMessage` value in
140
+ `scripts/translate.config.json` (default `docs: update translations`) so that
141
+ subsequent incremental runs can detect the baseline commit and only
142
+ re-translate the files that changed since.
@@ -0,0 +1,105 @@
1
+ ---
2
+ title: Python Agent to A2A Agent
3
+ description: Connect a Python Agent to a remote A2A agent
4
+ when:
5
+ sourceType: py#agent
6
+ targetType:
7
+ - ts#agent
8
+ - py#agent
9
+ ---
10
+ import { FileTree } from '@astrojs/starlight/components';
11
+ import Link from '@components/link.astro';
12
+ import RunGenerator from '@components/run-generator.astro';
13
+ import GeneratorParameters from '@components/generator-parameters.astro';
14
+ import NxCommands from '@components/nx-commands.astro';
15
+ import Snippet from '@components/snippet.astro';
16
+
17
+ The `connection` generator can connect your <Link path="guides/py-agent">Python Agent</Link> to a remote [A2A](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-a2a.html) agent — either <Link path="guides/ts-agent">TypeScript</Link> or <Link path="guides/py-agent">Python</Link> — so your agent can delegate to another agent as a tool.
18
+
19
+ The generator sets up all the necessary wiring so your agent can discover and invoke the remote A2A agent, both when deployed to AWS (via Bedrock AgentCore) and when running locally.
20
+
21
+ ## Prerequisites
22
+
23
+ Before using this generator, ensure you have:
24
+
25
+ 1. A Python project with a <Link path="guides/py-agent">Strands Agent</Link> component (any protocol)
26
+ 2. A project with an Agent component generated with `--protocol=A2A` and `--auth=IAM` (either <Link path="guides/ts-agent">`ts#agent`</Link> or <Link path="guides/py-agent">`py#agent`</Link>)
27
+ 3. Both components created with `computeType: BedrockAgentCoreRuntime`
28
+
29
+ ## Usage
30
+
31
+ ### Run the Generator
32
+
33
+ <RunGenerator generator="connection" />
34
+
35
+ Select your host agent project as the source and your A2A agent project as the target. If your projects contain multiple components, specify the `sourceComponent` and `targetComponent` options to disambiguate.
36
+
37
+ ### Options
38
+
39
+ <GeneratorParameters generator="connection" />
40
+
41
+ ## Generator Output
42
+
43
+ The generator creates a shared `agent_connection` Python project at `packages/common/agent_connection/` (if it doesn't already exist). Per-connection client modules are generated into this shared project:
44
+
45
+ <FileTree>
46
+
47
+ - packages/common/agent\_connection
48
+ - \<scope>\_agent\_connection
49
+ - \_\_init\_\_.py Re-exports per-connection clients
50
+ - core
51
+ - agentcore\_a2a\_client.py Core AgentCore A2A client with SigV4 authentication
52
+ - app
53
+ - \<target\_agent\_name>\_client.py Per-connection client for each A2A agent
54
+
55
+ </FileTree>
56
+
57
+ Additionally, the generator:
58
+ - Transforms your agent's `agent.py` to register the remote A2A agent as a tool using `@tool`
59
+ - Adds the `agent_connection` project as a workspace dependency of your agent project
60
+ - Updates the agent's `serve-local` target to depend on the target agent's `serve-local` target
61
+
62
+ ## Using the Connected A2A Agent
63
+
64
+ The generator transforms your agent's `agent.py` to wrap the remote A2A agent as a tool:
65
+
66
+ ```python title="packages/my-project/my_module/agent/agent.py" {4,9-15,21}
67
+ from contextlib import contextmanager
68
+ from strands import Agent, tool
69
+
70
+ from my_scope_agent_connection import RemoteAgentClient
71
+
72
+ @contextmanager
73
+ def get_agent(session_id: str):
74
+ remote_agent = RemoteAgentClient.create(session_id=session_id)
75
+
76
+ @tool
77
+ def ask_remote_agent(prompt: str) -> str:
78
+ """Delegate a question to the remote RemoteAgent A2A agent and return its reply."""
79
+ return str(remote_agent(prompt))
80
+
81
+ yield Agent(
82
+ system_prompt="...",
83
+ tools=[ask_remote_agent],
84
+ )
85
+ ```
86
+
87
+ The `session_id` parameter is plumbed through from the caller, ensuring consistency for [Bedrock AgentCore Observability](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/observability.html).
88
+
89
+ Under the hood, `RemoteAgentClient.create(session_id=...)` returns a Strands `A2AAgent` configured with an `httpx.AsyncClient` that signs requests with SigV4 when deployed to AWS, and a plain `http://localhost:<port>/` endpoint when `SERVE_LOCAL=true`.
90
+
91
+ ## Infrastructure
92
+
93
+ <Snippet name="connection/a2a-infrastructure" parentHeading="Infrastructure" />
94
+
95
+ ## Local Development
96
+
97
+ The generator configures the host agent's `serve-local` target to:
98
+ 1. Start the connected A2A agent(s) automatically
99
+ 2. Set `SERVE_LOCAL=true` so the generated client connects directly to `http://localhost:<port>/` instead of AgentCore
100
+
101
+ Run the agent locally with:
102
+
103
+ <NxCommands commands={["<agent-name>-serve-local <project-name>"]} />
104
+
105
+ This will start both the host agent and all connected A2A agents, with the host agent calling the remote agents over plain HTTP on their assigned local ports.
@@ -0,0 +1,147 @@
1
+ ---
2
+ title: Python Agent to MCP
3
+ description: Connect a Python Agent to an MCP server
4
+ when:
5
+ sourceType: py#agent
6
+ targetType:
7
+ - ts#mcp-server
8
+ - py#mcp-server
9
+ ---
10
+ import { FileTree } from '@astrojs/starlight/components';
11
+ import Link from '@components/link.astro';
12
+ import RunGenerator from '@components/run-generator.astro';
13
+ import GeneratorParameters from '@components/generator-parameters.astro';
14
+ import NxCommands from '@components/nx-commands.astro';
15
+ import Infrastructure from '@components/infrastructure.astro';
16
+
17
+ The `connection` generator can connect your <Link path="guides/py-agent">Python Agent</Link> to an MCP server (either <Link path="guides/ts-mcp-server">TypeScript</Link> or <Link path="guides/py-mcp-server">Python</Link>).
18
+
19
+ The generator sets up all the necessary wiring so your agent can discover and invoke the MCP server's tools, both when deployed to AWS (via Bedrock AgentCore) and when running locally.
20
+
21
+ ## Prerequisites
22
+
23
+ Before using this generator, ensure you have:
24
+
25
+ 1. A Python project with a <Link path="guides/py-agent">Strands Agent</Link> component
26
+ 2. A project with an MCP server component (either <Link path="guides/ts-mcp-server">`ts#mcp-server`</Link> or <Link path="guides/py-mcp-server">`py#mcp-server`</Link>)
27
+ 3. Both components created with `computeType: BedrockAgentCoreRuntime`
28
+
29
+ ## Usage
30
+
31
+ ### Run the Generator
32
+
33
+ <RunGenerator generator="connection" />
34
+
35
+ Select your agent project as the source and your MCP server project as the target. If your projects contain multiple components, specify the `sourceComponent` and `targetComponent` options to disambiguate.
36
+
37
+ ### Options
38
+
39
+ <GeneratorParameters generator="connection" />
40
+
41
+ ## Generator Output
42
+
43
+ The generator creates a shared `agent_connection` Python project at `packages/common/agent_connection/` (if it doesn't already exist). Per-connection client modules are generated into this shared project:
44
+
45
+ <FileTree>
46
+
47
+ - packages/common/agent\_connection
48
+ - \<scope>\_agent\_connection
49
+ - \_\_init\_\_.py Re-exports per-connection clients
50
+ - core
51
+ - agentcore\_mcp\_client.py Core AgentCore MCP client
52
+ - app
53
+ - \<mcp\_server\_name>\_client.py Per-connection client for each MCP server
54
+
55
+ </FileTree>
56
+
57
+ Additionally, the generator:
58
+ - Transforms your agent's `agent.py` to import and use the MCP server's tools via a class-based client
59
+ - Adds the `agent_connection` project as a workspace dependency of your agent project
60
+ - Updates the agent's `serve-local` target to depend on the MCP server's serve target
61
+
62
+ ## Using the Connected MCP Server
63
+
64
+ The generator transforms your agent's `agent.py` to use the MCP server's tools:
65
+
66
+ ```python title="packages/my-project/my_module/agent/agent.py" {4,10-14}
67
+ from contextlib import contextmanager
68
+ from strands import Agent
69
+
70
+ from my_scope_agent_connection import MyMcpServerClient
71
+
72
+ @contextmanager
73
+ def get_agent(session_id: str):
74
+ my_mcp_server = MyMcpServerClient.create(session_id=session_id)
75
+ with (
76
+ my_mcp_server,
77
+ ):
78
+ yield Agent(
79
+ system_prompt="...",
80
+ tools=[*my_mcp_server.list_tools_sync()],
81
+ )
82
+ ```
83
+
84
+ The `session_id` parameter is plumbed through from the caller, ensuring consistency for [Bedrock AgentCore Observability](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/observability.html).
85
+
86
+ ## Infrastructure
87
+
88
+ <Infrastructure>
89
+ <Fragment slot="cdk">
90
+ After running the connection generator, you need to grant the agent permission to invoke the MCP server:
91
+
92
+ ```ts title="packages/infra/src/stacks/application-stack.ts" {5}
93
+ const mcpServer = new MyMcpServer(this, 'MyMcpServer');
94
+ const myAgent = new MyAgent(this, 'MyAgent');
95
+
96
+ // Grant the agent permissions to invoke the MCP server
97
+ mcpServer.grantInvokeAccess(myAgent);
98
+ ```
99
+
100
+ The MCP server's AgentCore runtime ARN is automatically registered in the `agentcore` namespace of <Link path="guides/runtime-config">Runtime Configuration</Link> by the generated CDK construct, so the agent can discover it at runtime.
101
+ </Fragment>
102
+ <Fragment slot="terraform">
103
+ After running the connection generator, you need to grant the agent permission to invoke the MCP server in your Terraform configuration:
104
+
105
+ ```hcl title="packages/infra/src/main.tf" {12-24}
106
+ module "inventory_mcp_server" {
107
+ source = "../../common/terraform/src/app/mcp-servers/inventory-mcp"
108
+ }
109
+
110
+ module "story_agent" {
111
+ source = "../../common/terraform/src/app/agents/story-agent"
112
+ }
113
+
114
+ # Grant the agent permissions to invoke the MCP server
115
+ resource "aws_iam_policy" "agent_invoke_mcp" {
116
+ name = "AgentInvokeMcpPolicy"
117
+ policy = jsonencode({
118
+ Version = "2012-10-17"
119
+ Statement = [{
120
+ Effect = "Allow"
121
+ Action = "bedrock-agentcore:InvokeAgent"
122
+ Resource = module.inventory_mcp_server.agent_core_runtime_arn
123
+ }]
124
+ })
125
+ }
126
+
127
+ resource "aws_iam_role_policy_attachment" "agent_invoke_mcp" {
128
+ role = module.story_agent.agent_core_runtime_role_arn
129
+ policy_arn = aws_iam_policy.agent_invoke_mcp.arn
130
+ }
131
+ ```
132
+
133
+ The MCP server's AgentCore runtime ARN is automatically registered in the `agentcore` namespace of <Link path="guides/runtime-config">Runtime Configuration</Link> by the generated Terraform module, so the agent can discover it at runtime.
134
+ </Fragment>
135
+ </Infrastructure>
136
+
137
+ ## Local Development
138
+
139
+ The generator configures the agent's `serve-local` target to:
140
+ 1. Start the connected MCP server(s) automatically
141
+ 2. Set `SERVE_LOCAL=true` so the generated client uses direct HTTP transport instead of AgentCore
142
+
143
+ Run the agent locally with:
144
+
145
+ <NxCommands commands={["<agent-name>-serve-local <project-name>"]} />
146
+
147
+ This will start both the agent and all connected MCP servers, with the agent connecting to the MCP servers directly via HTTP on their assigned local ports.