Technical Implementation: Enforcing Security for Coding Agents with MCP
This section provides a step-by-step guide on how to integrate the GitGuardian MCP server directly into GitHub Copilot coding agent's configuration. This setup allows the agent to use the secret_scan tool to perform real-time security checks, ensuring code is secure before it is committed to a Pull Request branch and reviewed by humans.
If you just want to see the results, you can skip to the Demonstration section below.
1. Repository Setup
The first step is to establish an environment for the integration. In this example, we will set up a new empty repository in GitHub.
Repository setup
2. GitGuardian MCP Server Configuration
To integrate the MCP server, we need to add it to the agent's configuration and ensure the agent has the necessary permissions and network access.
We will add the GitGuardian MCP server to the Copilot coding agent configuration as shown below, referencing an environment secret for the personal access token variable (we will create this later).
{"mcpServers":{"GitGuardian":{"type":"stdio","command":"uvx","args":["--from","git+https://github.com/GitGuardian/ggmcp.git","developer-mcp-server"],"env":{"ENABLE_LOCAL_OAUTH":"false","GITGUARDIAN_PERSONAL_ACCESS_TOKEN":"COPILOT_MCP_GITGUARDIAN_PERSONAL_ACCESS_TOKEN"},"tools":["scan_secrets"]}}}
MCP server configuration
Next, add https://api.gitguardian.com and https://dashboard.gitguardian.com to the Copilot coding agent internet access custom allowlist.
Firewall allowlist
3. Service Account and Secret Management
To authenticate the agent's security scans, a dedicated GitGuardian service account with minimal permissions is required.
We can set do this in the GitGuardian settings. Create a new service account, and give it "scan" permissions.
Service account setup
Use the button at the bottom to create the service account and save the new service account's token for a later step.
4. Configuring the Environment Secret
The service account's token must be securely stored as an environment secret so that it's only accessible by the Copilot agent's MCP config.
Go to the GitHub repo's environment settings and navigate to the copilot environment or create one if it doesn't exist.
Copilot environment settings
Add the environment secret we referenced earlier named COPILOT_MCP_GITGUARDIAN_PERSONAL_ACCESS_TOKEN, and paste the value of the service account token that was created in step 3.
Adding the environment secret
Secret saved
5. Agent Instructions
The final piece of the setup is instructing the Copilot agent to use the new security tool as part of its standard workflow.
Create a Copilot instructions document that tells the agent to check all modified code with the secret_scan tool.
Agent instructions
The GitGuardian MCP server is now set up and ready to be used by the Copilot coding agent.
Demonstration: MCP Security Tools in Action
To validate the MCP integration and Copilot's adherence to our new security rules, we can observe the agent's behavior during a typical development task.
1. Assign a task to Copilot
First, we will ask Copilot to generate code by creating an issue and assigning it to Copilot. In this example, we are asking for a boilerplate Flask API that supports authentication.
Creating an issue for Copilot
For demonstration purposes, we will explicitly ask Copilot to hardcode the secret key (this is a contrived example to force a finding, but hardcoded secrets may occur without explicit instructions).
Asking Copilot to hardcode the key
2. Observe Copilot's behavior
Once assigned a task, Copilot will create a draft PR to track its work. Navigate to the PR and view the coding session to observe its activity in real time.
Copilot draft PR
When the session kicks off, we can see the GitGuardian MCP server starting up.
MCP server starting up
As the agent implements the Flask API, we can see it has hardcoded the secret key.
Hardcoded secret key
Once Copilot is done making changes, it calls the secret_scan tool as instructed and finds the hardcoded secret key.
Secret scan finding
Because we explicitly asked Copilot to hardcode the secret to demonstrate this example, the agent only adds warnings instead of actually remediating the issue. In a real scenario, Copilot would not have conflicting instructions about how to handle the secret findings and would remediate the issue automatically.
Conclusion
In this blog post, we demonstrated how GitGuardian MCP can be used to shift security left in the absence of traditional security tools like IDE plugins. While hardcoded secrets are a prevalent and critical finding, the challenge of securing AI-generated code extends beyond secret exposure. This approach of providing agents with state-of-the-art security tools should be replicated to automate the detection and resolution of many issues.
Agents, like humans, aren't perfect, but we can secure AI-generated code. By embedding security directly into the AI agent's control plane and instructions, organizations can enforce security checks at the earliest possible stage, significantly accelerating the safety and productivity of agentic software development.
GitGuardian Interactive Demo