Write scripts and tests for MCP requests
You can add scripts to your Model Context Protocol (MCP) requests to set up a connection, test what a server exposes, and test every tool call, prompt, and resource read. Scripts run in the Postman Sandbox, so you can use the pm object, variables, the Postman Vault, pm.sendRequest, and package library imports, the same as in HTTP scripts.
Use scripts with MCP requests to do the following:
- Set the server URL, headers, or command before Postman connects.
- Check the server’s name, protocol version, and capabilities.
- Check that the server exposes the tools, prompts, and resources you expect, with the input schemas you expect.
- Test the result of each call and save values for later calls.
- Clean up tokens and IDs when the session ends.
- Run MCP requests in the Collection Runner next to HTTP and other requests.
How MCP scripts run
An MCP request is a session: Postman connects to the server, you send one or more calls, and then Postman disconnects. Scripts attach to four points in that session, called hooks. They run in this order:
Keep the following in mind about when scripts run:
- Postman reads the request’s current scripts on every call. If you edit an After response script while you’re connected, the change applies to the next call. You don’t need to reconnect.
- When you enter a URL or command and Postman loads the server’s tools, prompts, and resources for you, no connection scripts run. Connection scripts run when you click Connect or send a call.
- If you call
pm.execution.skipRequest()in a Before connect script, Postman doesn’t connect to the server. No later scripts run. If a Before connect script fails, Postman also doesn’t connect. - After response scripts run after each tool call, prompt, and resource read that you send. They don’t run for calls made by MCP Apps.
- After disconnect scripts run after a disconnect that the client asked for.
pm.sendRequestalways sends an HTTP request, even in a script for an MCP request. To learn more, see pm.sendRequest.- In
pm.info.eventName, the hooks arebeforeConnect,afterConnect,afterResponse, andafterDisconnect. To learn more, see pm.info.
Add a script to an MCP request
To add a script to an MCP request, do the following:
- Open an MCP request.
- Click the Scripts tab.
- Select a hook: Before connect, After connect, After response, or After disconnect.
- Write your script in the editor, or select a snippet from the snippets list.
- Save the request.
The snippets for MCP requests include the following:
- Set an
Authorizationheader. - Test the server name and protocol version.
- Test that tools are exposed.
- Test a tool’s input schema.
- Test that a call succeeded or reported an error.
- Test structured content.
- Test response time.
The pm object in MCP scripts
The values of pm.request and pm.response depend on the hook:
Connection properties
In Before connect, After connect, and After disconnect, pm.request describes the connection:
Server properties
In After connect, pm.response describes the server:
Call properties
In After response, pm.request and pm.response describe the call:
Examples
Set an authorization header before connecting
Test what the server exposes
Test one tool’s response
An After response script runs after every call in the session. To test a single tool, check the method and name first:
Save a value for the next call
Use {{ticketId}} in the arguments of the next call.
Clean up when the session ends
View test results
Test results appear in the Test Results tab in the response pane. Postman groups the session’s tests as follows:
- Connection for tests in After connect.
- One group per call, labeled with the tool name and its first argument.
- Disconnection for tests in After disconnect.
Add MCP scripts to a collection or folder
Collection and folder scripts run for every MCP request inside them. Use them to set authentication once, or to run the same checks against every server in a collection.
To add MCP scripts to a collection or folder, do the following:
- Open the collection or folder and click the Scripts tab.
- Select an MCP hook. MCP hooks have an
mcp:prefix:mcp:beforeConnect,mcp:afterConnect,mcp:afterResponse, andmcp:afterDisconnect. - Write your script and save.
MCP hooks run only for MCP requests. HTTP and other request types in the same collection keep using their own hooks.
Run MCP requests in the Collection Runner
The Collection Runner runs MCP requests with their scripts and tests, in the same run as HTTP and other request types. Values you set in one request’s scripts are available to the requests that follow, so a single run can call an API and then check the MCP tool that depends on it.
A STDIO MCP server starts a process on your machine. Before you can run a STDIO request in the Collection Runner, you must approve it from the request first. The run won’t start until you approve every STDIO request in the run.
To learn more, see Run collections.
Run MCP scripts and tests from the command line
You can also run MCP requests with their scripts and tests using the Postman CLI. To learn more, see Postman CLI MCP commands and Run a collection with the Postman CLI.