|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Sample: an MCP server governed by Microsoft's Agent Governance Toolkit with the AWS Bedrock backends |
| 5 | +// layered on top. It follows the primary path from the AWS.Bedrock.MAG README: |
| 6 | +// |
| 7 | +// AddMcpServer().WithGovernance(...).WithBedrockGovernance(...) |
| 8 | +// |
| 9 | +// .WithGovernance(...) comes from Microsoft.AgentGovernance.Extensions.ModelContextProtocol and |
| 10 | +// registers the toolkit's GovernanceKernel + policy engine on the MCP server. |
| 11 | +// .WithBedrockGovernance(...) comes from AWS.Bedrock.MAG and attaches the Bedrock Guardrails policy |
| 12 | +// backend, Bedrock PII sanitization, and the CloudWatch audit sink. |
| 13 | +// |
| 14 | +// See README.md for prerequisites, IAM, and how to drive it. |
| 15 | + |
| 16 | +using Amazon; |
| 17 | +using AgentGovernance.Extensions.ModelContextProtocol; |
| 18 | +using BedrockGovernedMcpServer.Tools; |
| 19 | +using Microsoft.Extensions.DependencyInjection; |
| 20 | +using Microsoft.Extensions.Hosting; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | + |
| 23 | +var builder = Host.CreateApplicationBuilder(args); |
| 24 | + |
| 25 | +// A stdio MCP server speaks the protocol over stdout, so all logging MUST go to stderr — otherwise log |
| 26 | +// lines corrupt the protocol stream. |
| 27 | +builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace); |
| 28 | + |
| 29 | +// Region and guardrail come from the environment so the sample stays runnable without editing code. |
| 30 | +var region = RegionEndpoint.GetBySystemName( |
| 31 | + Environment.GetEnvironmentVariable("MAG_TEST_REGION") ?? "us-west-2"); |
| 32 | +var guardrailId = Environment.GetEnvironmentVariable("MAG_GUARDRAIL_ID"); |
| 33 | + |
| 34 | +builder.Services |
| 35 | + .AddMcpServer() |
| 36 | + .WithStdioServerTransport() |
| 37 | + // Microsoft's toolkit: loads YAML policy and stands up the GovernanceKernel this server governs with. |
| 38 | + .WithGovernance(o => |
| 39 | + { |
| 40 | + o.PolicyPaths.Add("policies/mcp.yaml"); |
| 41 | + o.ServerName = "bedrock-governed-sample"; |
| 42 | + o.DefaultAgentId = "did:mcp:sample-agent"; |
| 43 | + |
| 44 | + // The toolkit requires an authenticated agent identity by default and denies every call before it |
| 45 | + // reaches the Bedrock backend. For a self-contained sample we allow the DefaultAgentId fallback. |
| 46 | + // In production, leave this on and set an AgentIdResolver that maps your authenticated principals. |
| 47 | + o.RequireAuthenticatedAgentId = false; |
| 48 | + }) |
| 49 | + // AWS backends. Two modes, chosen by whether you supply a pre-created guardrail: |
| 50 | + // MAG_GUARDRAIL_ID set -> full path: guardrail-based policy + PII sanitization of tool output + audit. |
| 51 | + // MAG_GUARDRAIL_ID unset -> inline-checks policy (InvokeGuardrailChecks, no guardrail resource) with |
| 52 | + // PII sanitization off, since inline checks detect but do not mask text. |
| 53 | + .WithBedrockGovernance(o => |
| 54 | + { |
| 55 | + o.Region = region; |
| 56 | + o.Audit.LogGroupName = "/agent-governance/bedrock-sample"; |
| 57 | + |
| 58 | + if (!string.IsNullOrWhiteSpace(guardrailId)) |
| 59 | + { |
| 60 | + o.Policy.GuardrailId = guardrailId; // ApplyGuardrail on tool-call input. |
| 61 | + o.EnablePiiSanitization = true; // Reuses the policy guardrail to redact tool-output PII. |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + o.EnablePiiSanitization = false; |
| 66 | + o.Policy.InlineChecks = new AWS.Bedrock.MAG.GuardrailChecksOptions |
| 67 | + { |
| 68 | + // PII detection on the (JSON) tool-call context: a call whose arguments contain an SSN or |
| 69 | + // email is denied. This is the reliable inline-check demo. |
| 70 | + SensitiveInformationEntities = { "US_SOCIAL_SECURITY_NUMBER", "EMAIL" }, |
| 71 | + ConfidenceThreshold = 0.5, |
| 72 | + |
| 73 | + // NOTE: content-filter and prompt-attack categories are intentionally left off here. By |
| 74 | + // default the tool-call context is serialized to compact JSON, and the prompt-attack |
| 75 | + // classifier reads that structured JSON as an injection attempt and denies benign calls. To |
| 76 | + // use those categories, project the context to prose first via Policy.ContextSerializer, e.g.: |
| 77 | + // o.Policy.ContextSerializer = ctx => $"Tool {ctx["tool"]} called with {string.Join(", ", ctx)}"; |
| 78 | + // and then add: |
| 79 | + // PromptAttackCategories = { "PROMPT_INJECTION", "JAILBREAK" }, |
| 80 | + // ContentFilterCategories = { "HATE", "INSULTS", "VIOLENCE" }, SeverityThreshold = 0.5, |
| 81 | + }; |
| 82 | + } |
| 83 | + }) |
| 84 | + .WithTools<SupportTools>(); |
| 85 | + |
| 86 | +await builder.Build().RunAsync(); |
0 commit comments