Bridging the gap of llm-assisted secure coding: context-aware rule narrowing with CodeGuard and an MCP-as-a-judge server
Keywords
- large language models
- secure code generation
- Model Context Protocol
- retrieval-augmented policy
- rule narrowing
- LLM guardrails
- AI pair programming
- security compliance
- prompt optimization
- context-aware filtering
- CodeGuard
- MCP-as-a-judge
Abstract
Large Language Model (LLM) coding assistants are increasingly embedded in professional software development workflows, where they must respect the same security policies that govern human engineers. The CodeGuard project – a curated corpus of more than twenty rule files spanning cryptography, input validation, authentication, infrastructure-as-code, and languagespecific hardening – offers a comprehensive baseline for such policies. However, loading the full corpus into every model turn is impractical: it inflates prompt size, dilutes the model's attention across rules that do not apply to the current change, and ultimately weakens compliance with the rules that do apply. I present a context-aware narrowing approach in which a local Model Context Protocol (MCP) server, codeguard-local, acts as a deterministic judge over the rule corpus. Given the user's request and the workspace-relative file paths the model intends to touch, the server returns only the top-N rules – together with their hard "must" constraints – that are semantically and syntactically relevant to the current turn. This "MCP-as-a-judge" pattern decouples policy curation from policy enforcement, keeps the assistant's prompt budget under control, and produces measurable improvements in the relevance of the security guidance the model surfaces to developers. I describe the architecture, the rule-selection contract, and its integration with the GitHub Copilot agent surface, and I discuss the trade-offs of judge-based narrowing against full-context prompting and fine-tuned guardrail models such as PromptShield.
References
Raihan, N., Erdachew, N., Devi, J., Santos, J. C. S., & Zampieri, M. (2026). CodeGuard: Improving LLM guardrails in CS education (arXiv:2602.02509). arXiv. https://arxiv.org/abs/2602.02509
Inan, H., Upasani, K., Chi, J., Rungta, R., Iyer, K., Mao, Y., Tontchev, M., Hu, Q., Fuller, B., Testuggine, D., & Khabsa, M. (2023). Llama Guard: LLM-based input-output safeguard for human-AI conversations (arXiv:2312.06674). arXiv. https://arxiv.org/abs/2312.06674
Rebedea, T., Dinu, R., Sreedhar, M. N., Parisien, C., & Cohen, J. (2023). NeMo Guardrails: A toolkit for controllable and safe LLM applications with programmable rails. In Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing: System Demonstrations (pp. 431–445). Association for Computational Linguistics. https://doi.org/10.18653/v1/2023.emnlp-demo.40
Ghosh, S., Varshney, P., Sreedhar, M. N., Padmakumar, A., Rebedea, T., Varghese, J. R., & Parisien, C. (2025). AEGIS 2.0: A diverse AI safety dataset and risks taxonomy for alignment of LLM guardrails (arXiv:2501.09004). arXiv. https://arxiv.org/abs/2501.09004
Anthropic. (2024, November 25). Introducing the Model Context Protocol. https://www.anthropic.com/news/model-context-protocol
OWASP Foundation. (2024). OWASP Top 10: A standard awareness document for developers and web application security. https://owasp.org/Top10/
Harband, J., & Roguski, P. (2026, April 15). Bridging the gap in product lifecycle management: How OpenEoX and CLE work together. OWASP Foundation. https://owasp.org/blog/2026/04/15/end-of-life-cle-and-openeox
Pearce, H., Ahmad, B., Tan, B., Dolan-Gavitt, B., & Karri, R. (2022). Asleep at the keyboard? Assessing the security of GitHub Copilot's code contributions. In 2022 IEEE Symposium on Security and Privacy (SP) (pp. 754–768). IEEE. https://doi.org/10.1109/SP46214.2022.9833571
Sandoval, G., Pearce, H., Nys, T., Karri, R., Garg, S., & Dolan-Gavitt, B. (2023). Lost at C: A user study on the security implications of large language model code assistants. In Proceedings of the 32nd USENIX Security Symposium (pp. 2205–2222). USENIX Association. https://www.usenix.org/conference/usenixsecurity23/presentation/sandoval
Perry, N., Srivastava, M., Kumar, D., & Boneh, D. (2023). Do users write more insecure code with AI assistants? In Proceedings of the 2023 ACM SIGSAC Conference on Computer and Communications Security (pp. 2785–2799). Association for Computing Machinery. https://doi.org/10.1145/3576915.3623157
Liu, N. F., Lin, K., Hewitt, J., Paranjape, A., Bevilacqua, M., Petroni, F., & Liang, P. (2024). Lost in the middle: How language models use long contexts. Transactions of the Association for Computational Linguistics, 12, 157–173. https://doi.org/10.1162/tacl_a_00638
Lewis, P., Perez, E., Piktus, A., Petroni, F., Karpukhin, V., Goyal, N., Küttler, H., Lewis, M., Yih, W., Rocktäschel, T., Riedel, S., & Kiela, D. (2020). Retrieval-augmented generation for knowledge-intensive NLP tasks. In Advances in Neural Information Processing Systems (Vol. 33, pp. 9459–9474). Curran Associates. https://arxiv.org/abs/2005.11401
Karpukhin, V., Oğuz, B., Min, S., Lewis, P., Wu, L., Edunov, S., Chen, D., & Yih, W. (2020). Dense passage retrieval for open-domain question answering. In Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP) (pp. 6769–6781). Association for Computational Linguistics. https://doi.org/10.18653/v1/2020.emnlp-main.550
Reimers, N., & Gurevych, I. (2019). Sentence-BERT: Sentence embeddings using Siamese BERT-networks. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP) (pp. 3982–3992). Association for Computational Linguistics. https://doi.org/10.18653/v1/D19-1410
Wei, J., Wang, X., Schuurmans, D., Bosma, M., Ichter, B., Xia, F., Chi, E., Le, Q., & Zhou, D. (2022). Chain-of-thought prompting elicits reasoning in large language models. In Advances in Neural Information Processing Systems (Vol. 35, pp. 24824–24837). Curran Associates. https://arxiv.org/abs/2201.11903
Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., & Cao, Y. (2023). ReAct: Synergizing reasoning and acting in language models. In Proceedings of the 11th International Conference on Learning Representations (ICLR). https://arxiv.org/abs/2210.03629
National Institute of Standards and Technology. (2024). Module-lattice-based key-encapsulation mechanism standard (FIPS Publication 203). U.S. Department of Commerce. https://doi.org/10.6028/NIST.FIPS.203
National Institute of Standards and Technology. (2024). Module-lattice-based digital signature standard (FIPS Publication 204). U.S. Department of Commerce. https://doi.org/10.6028/NIST.FIPS.204
Open Source Security Foundation. (2023). Supply-chain Levels for Software Artifacts (SLSA) specification (Version 1.0). Linux Foundation. https://slsa.dev/spec/v1.0/