Recent advances in the Model Context Protocol (MCP) have enabled large language models (LLMs) to invoke external tools with unprecedented ease. This creates a new class of powerful and tool augmented agents. Unfortunately, this capability also introduces an under explored attack surface, specifically the malicious manipulation of tool responses. Existing techniques for indirect prompt injection that target MCP suffer from high deployment costs, weak semantic coherence, or heavy white box requirements. Furthermore, they are often easily detected by recently proposed defenses. In this paper, we propose Tree structured Injection for Payloads (TIP), a novel black-box attack which generates natural payloads to reliably seize control of MCP enabled agents even under defense. Technically, We cast payload generation as a tree structured search problem and guide the search with an attacker LLM operating under our proposed coarse-to-fine optimization framework. To stabilize learning and avoid local optima, we introduce a path-aware feedback mechanism that surfaces only high quality historical trajectories to the attacker model. The framework is further hardened against defensive transformations by explicitly conditioning the search on observable defense signals and dynamically reallocating the exploration budget. Extensive experiments on four mainstream LLMs show that TIP attains over 95% attack success in undefended settings while requiring an order of magnitude fewer queries than prior adaptive attacks. Against four representative defense approaches, TIP preserves more than 50% effectiveness and significantly outperforms the state-of-the-art attacks. By implementing the attack on real world MCP systems, our results expose an invisible but practical threat vector in MCP deployments. We also discuss potential mitigation approaches to address this critical security gap.
Figures & tables
Fig. 1: Attack scenario overview. A client relies on a third-party Model Context Protocol (MCP) server to provide structured inputs to its locally deployed LLM-based tool. An adversary compromising the MCP server delivers a stealthy update containing a semantically coherent prompt injection embedded within legitimate response fields. The local LLM interprets this adversarial context as trusted input, executing malicious instructions while bypassing client-side defenses due to the payload’s syntactic and semantic plausibility.
Fig. 2: Overview of the TIP framework for generating stealthy, transferable JSON payloads through a tree-based search. It includes three main stages: Branch , where tool response simulation and a dual coarse-to-fine strategy refine payload intent and structure; Prune , which evaluates candidates via transferability checks (e.g., Monte Carlo scoring) across diverse instructions and models; and Feedback & Iteration , optimizing payloads using path-aware feedback from historical high-scoring nodes.
Defense
Method
Metric
Fraud
Data Steal
Product
Weather
Book
Ship
No Defense
Fixed
ASR
16.0%
45.0%
10.2%
0.0%
Query
-
-
-
-
TAP
ASR
91.0%
90.0%
0.0%
0.0%
Query
2580
2480
2580
2580
TIP (Ours)
ASR
100.0%
100.0%
100.0%
95.0%
TABLE I: Attack Success Rates (ASR) and Query Counts across different attack methods. TIP consistently outperforms baselines in both success rate and query efficiency across all defense settings.
Defense
Model
GetProduct
GetWeather
ExpediaBooking
ShipManager
Fixed
TAP
Ours
Fixed
TAP
Ours
Fixed
TAP
Ours
Fixed
TAP
Ours
No Defense
Llama3.1-8B-Instruct
3.3%
9.0%
97.1%
1.1%
0.0%
97.7%
85.9%
54.7%
100.0%
8.2%
2.0%
96.0%
Qwen2.5-72B-Instruct
31.0%
98.0%
100.0%
64.0%
92.0%
100.0%
43.0%
0.0%
100.0%
8.1%
0.0%
100.0%
Llama3.3-70B-Instruct
2.0%
0.0%
98.0%
2.0%
39.0%
100.0%
0.0%
0.0%
100.0%
52.0%
0.0%
100.0%
Instruction Prevention
Llama3.1-8B-Instruct
8.2%
13.6%
70.1%
4.3%
6.8%
81.5%
59.6%
4.8%
53.1%
0.0%
0.0%
15.5%
Qwen2.5-72B-Instruct
21.0%
49.0%
95.0%
52.0%
94.0%
92.0%
1.0%
0.0%
91.4%
0.0%
0.0%
42.0%
TABLE II: Transferability of attack effectiveness across different victim models. Payloads were trained on a small model ensemble (Qwen2.5/InternLM/GLM) and tested against unseen target models . TIP demonstrates superior generalization compared to baselines.
Fig. 3: Cosine similarity between generated payloads and legitimate responses. TIP payloads exhibit higher semantic similarity to benign data, reducing the likelihood of detection.
Model
Variant #1
Variant #2
Variant #3
Full Design
Qwen2.5-7B-Instruct
0.0%
37.7%
73.0%
95.0%
Llama3.1-8B-Instruct
0.0%
63.2%
83.4%
96.0%
Qwen2.5-72B-Instruct
0.0%
1.0%
97.0%
100.0%
Llama3.3-70B-Instruct
0.0%
45.0%
61.0%
100.0%
TABLE III: Impact of Removing Individual Components from TIP on Attack Success Rates: Variant #1 ( w/o. Defense Awareness ), Variant #2 ( w/o. Strategy ) and Variant #3 ( w/o. Path Feedback ). Results indicate that removing any single component significantly degrades performance or transferability.
Fig. 4: Comparison of training optimization curves between the baseline TAP and our proposed TIP. TAP suffers from local convergence, i.e., performance drops after initial peak, while our proposed TIP maintains a stable upward trend due to path-aware feedback.
Fig. 5: Tree-Structured optimization process for attack payload generation. The figure illustrates the iterative expansion of the search tree where each node represents a candidate payload with an associated score ranging from 0 to 1. Nodes are color coded based on their scores with red indicating low scores and yellow indicating high scores. The optimization process explores diverse payloads through branching and pruning which aims to converge towards high scoring nodes that maximize attack success rates.
Fig. 6: Attack effectiveness in a real-world LM Studio with Qwen2.5-7B-Instruct-1M. The agent retrieves the weather data and, prompted by the TIP-generated payload, proactively cites the attacker’s phishing URL as the official source.
Fig. 7: Attack effectiveness in a real-world Visual Studio Code IDE with GPT-4o. Despite the safety alignment of GPT-4o, the model trusts the MCP tool context and embeds the malicious link directly into the developer’s workspace.
Shenzhen Institutes of Advanced Technology, Chinese Academy of Sciences · University of Chinese Academy of Sciences · Nanyang Technological University +3
Institute of Information Engineering, Chinese Academy of Sciences, Beijing, China · School of Cyber Security, University of Chinese Academy of Sciences, Beijing, China