|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +""" |
| 4 | +Example demonstrating MCP (Model Context Protocol) tools with the Responses API. |
| 5 | +
|
| 6 | +This example shows how to use MCP tools with different allowed_tools configurations: |
| 7 | +1. No filter (allows all tools from the MCP server) |
| 8 | +2. Wildcard "*" (explicitly allows all tools) |
| 9 | +3. Specific tool names (filters to only those tools) |
| 10 | +
|
| 11 | +Set up this example by starting a vLLM OpenAI-compatible server with MCP tools enabled. |
| 12 | +For example: |
| 13 | +vllm serve openai/gpt-oss-20b --enforce-eager --tool-server demo |
| 14 | +
|
| 15 | +Environment variables: |
| 16 | +- VLLM_ENABLE_RESPONSES_API_STORE=1 |
| 17 | +- VLLM_GPT_OSS_SYSTEM_TOOL_MCP_LABELS=code_interpreter,container |
| 18 | +- VLLM_GPT_OSS_HARMONY_SYSTEM_INSTRUCTIONS=1 |
| 19 | +""" |
| 20 | + |
| 21 | +from openai import OpenAI |
| 22 | +from utils import get_first_model |
| 23 | + |
| 24 | + |
| 25 | +def example_no_filter(): |
| 26 | + """Example with no allowed_tools filter - allows all tools.""" |
| 27 | + print("=" * 60) |
| 28 | + print("Example 1: No allowed_tools filter (allows all tools)") |
| 29 | + print("=" * 60) |
| 30 | + |
| 31 | + base_url = "http://0.0.0.0:8000/v1" |
| 32 | + client = OpenAI(base_url=base_url, api_key="empty") |
| 33 | + model = get_first_model(client) |
| 34 | + |
| 35 | + response = client.responses.create( |
| 36 | + model=model, |
| 37 | + input="Execute this code: print('Hello from Python!')", |
| 38 | + instructions="Use the Python tool to execute code.", |
| 39 | + tools=[ |
| 40 | + { |
| 41 | + "type": "mcp", |
| 42 | + "server_label": "code_interpreter", |
| 43 | + "server_url": "http://localhost:8888", |
| 44 | + # No allowed_tools specified - all tools are available |
| 45 | + } |
| 46 | + ], |
| 47 | + ) |
| 48 | + |
| 49 | + print(f"Status: {response.status}") |
| 50 | + print(f"Output: {response.output_text}") |
| 51 | + print() |
| 52 | + |
| 53 | + |
| 54 | +def example_wildcard(): |
| 55 | + """Example with allowed_tools=['*'] - explicitly allows all tools.""" |
| 56 | + print("=" * 60) |
| 57 | + print("Example 2: allowed_tools=['*'] (select all tools)") |
| 58 | + print("=" * 60) |
| 59 | + |
| 60 | + base_url = "http://0.0.0.0:8000/v1" |
| 61 | + client = OpenAI(base_url=base_url, api_key="empty") |
| 62 | + model = get_first_model(client) |
| 63 | + |
| 64 | + response = client.responses.create( |
| 65 | + model=model, |
| 66 | + input="Execute this code: print('Hello from Python with wildcard!')", |
| 67 | + instructions="Use the Python tool to execute code.", |
| 68 | + tools=[ |
| 69 | + { |
| 70 | + "type": "mcp", |
| 71 | + "server_label": "code_interpreter", |
| 72 | + "server_url": "http://localhost:8888", |
| 73 | + # Using "*" to explicitly allow all tools from this MCP server |
| 74 | + # This is equivalent to not specifying allowed_tools |
| 75 | + "allowed_tools": ["*"], |
| 76 | + } |
| 77 | + ], |
| 78 | + ) |
| 79 | + |
| 80 | + print(f"Status: {response.status}") |
| 81 | + print(f"Output: {response.output_text}") |
| 82 | + print() |
| 83 | + |
| 84 | + |
| 85 | +def example_specific_tools(): |
| 86 | + """Example with specific allowed_tools list - filters available tools. |
| 87 | +
|
| 88 | + Note: This example uses 'web_search_preview' (browser) which has multiple |
| 89 | + sub-tools: 'search', 'open', 'find'. The code_interpreter (python) doesn't |
| 90 | + have sub-tools, so filtering doesn't apply there. |
| 91 | + """ |
| 92 | + print("=" * 60) |
| 93 | + print("Example 3: allowed_tools=['search'] (filter browser to specific tools)") |
| 94 | + print("=" * 60) |
| 95 | + |
| 96 | + base_url = "http://0.0.0.0:8000/v1" |
| 97 | + client = OpenAI(base_url=base_url, api_key="empty") |
| 98 | + model = get_first_model(client) |
| 99 | + |
| 100 | + response = client.responses.create( |
| 101 | + model=model, |
| 102 | + input="Search for 'Python programming tutorials'", |
| 103 | + instructions="Use the browser tool to search.", |
| 104 | + tools=[ |
| 105 | + { |
| 106 | + "type": "mcp", |
| 107 | + "server_label": "web_search_preview", |
| 108 | + "server_url": "http://localhost:8888", |
| 109 | + # Browser has tools: 'search', 'open', 'find' |
| 110 | + # Only allow 'search' - blocks 'open' and 'find' |
| 111 | + "allowed_tools": ["search"], |
| 112 | + } |
| 113 | + ], |
| 114 | + ) |
| 115 | + |
| 116 | + print(f"Status: {response.status}") |
| 117 | + print(f"Output: {response.output_text}") |
| 118 | + print() |
| 119 | + |
| 120 | + |
| 121 | +def example_object_format(): |
| 122 | + """Example using object format for allowed_tools with browser tools.""" |
| 123 | + print("=" * 60) |
| 124 | + print("Example 4: allowed_tools with object format") |
| 125 | + print("=" * 60) |
| 126 | + |
| 127 | + base_url = "http://0.0.0.0:8000/v1" |
| 128 | + client = OpenAI(base_url=base_url, api_key="empty") |
| 129 | + model = get_first_model(client) |
| 130 | + |
| 131 | + response = client.responses.create( |
| 132 | + model=model, |
| 133 | + input="Search for 'machine learning' and open the first result", |
| 134 | + instructions="Use the browser tool.", |
| 135 | + tools=[ |
| 136 | + { |
| 137 | + "type": "mcp", |
| 138 | + "server_label": "web_search_preview", |
| 139 | + "server_url": "http://localhost:8888", |
| 140 | + # Object format with tool_names field |
| 141 | + # Can also include read_only and other fields |
| 142 | + # Browser has tools: 'search', 'open', 'find' |
| 143 | + "allowed_tools": { |
| 144 | + "tool_names": [ |
| 145 | + "search", |
| 146 | + "open", |
| 147 | + ], # Allow search and open, block find |
| 148 | + "read_only": False, |
| 149 | + }, |
| 150 | + } |
| 151 | + ], |
| 152 | + ) |
| 153 | + |
| 154 | + print(f"Status: {response.status}") |
| 155 | + print(f"Output: {response.output_text}") |
| 156 | + print() |
| 157 | + |
| 158 | + |
| 159 | +def main(): |
| 160 | + """Run all examples.""" |
| 161 | + print("\n" + "=" * 60) |
| 162 | + print("MCP Tools with allowed_tools Examples") |
| 163 | + print("=" * 60 + "\n") |
| 164 | + |
| 165 | + # Run all examples |
| 166 | + example_no_filter() |
| 167 | + example_wildcard() |
| 168 | + example_specific_tools() |
| 169 | + example_object_format() |
| 170 | + |
| 171 | + print("=" * 60) |
| 172 | + print("Summary:") |
| 173 | + print(" - No filter or '*' → All tools available from server") |
| 174 | + print(" - Specific list → Only those sub-tools available") |
| 175 | + print(" - Object format → More control with tool_names field") |
| 176 | + print("") |
| 177 | + print("Note: allowed_tools filters SUB-TOOLS within an MCP server:") |
| 178 | + print(" - code_interpreter (python): No sub-tools to filter") |
| 179 | + print(" - web_search_preview (browser): Has 'search', 'open', 'find'") |
| 180 | + print("=" * 60) |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + main() |
0 commit comments