MCP Server returns a response but remains running instead of shutting down

When the MCP Server (Coded)/command is invoked through the UiPath Agent tool, the server returns the expected response but does not shut down immediately afterward. The server remains running instead of terminating gracefully.

Hi @amol.bamane
Welcome to the Community

Check this

If it’s helpful please mark as solutions & Happy Automations

Thanks

Receiving the response successfully means the MCP server is working correctly.
The server remaining active is expected behavior unless explicit shutdown logic is implemented.

I have tried implementing shutdown logic, but still not working. If you have sample code of coded mcp server with shutdown ogic then please share it.

If you are starting with UiPath, then using PID you can stop it or terminate it.

Hi @amol.bamane

This is expected behavior. When the MCP Server (Coded) is started via the UiPath Agent tool, it runs as a long-lived process and won’t shut down automatically after returning a response. You need to explicitly stop it (for example, send a shutdown/exit command or kill the process) or design the server to terminate itself after handling the request.

If you have sample code of coded mcp server with shutdown logic then please share it. That would be helpful.

Hi Mona,

If you have any sample code of coded mcp server with shutdown logic then please share it. That would be helpful. Or else any documents related to it, please share with me.

Thanks & Regards,
Amol

This is expected behavior since coded MCP servers run as long-lived processes. They don’t stop automatically after returning a response unless you add shutdown logic.

You can terminate the server explicitly after handling the request, for example:

server.onRequest(async (req) => {
server.sendResponse(“done”);
setTimeout(() => process.exit(0), 100);
});

Alternatively, you can expose a separate shutdown command or handle signals like SIGTERM/SIGINT. There is no built-in auto shutdown for coded MCP servers.

Hi Mona,

I am using below code, could you please let me know you provided terminate example how will work here?

import math

from typing import Any, Dict, List

from mcp.server.fastmcp import FastMCP

mcp = FastMCP(“Advanced Math Operations Server”)

Basic arithmetic operations

@mcp.tool()

def add(a: float, b: float) → float:

return a + b

@mcp.tool()

def subtract(a: float, b: float) → float:

return a - b

Run the server when the script is executed

if name == “main”:

mcp.run()

Thanks & Regards,

Amol

Your MCP server stays running because mcp.run() starts a long-lived process. To make it terminate:

  1. Shutdown after a request: use threading.Timer to call sys.exit(0) after returning a response.
  2. Dedicated shutdown tool: add a shutdown() function that calls sys.exit(0) so you can stop the server explicitly when needed.

This ensures the server exits gracefully instead of running indefinitely.

I tried Option 1 from the suggested solution. After receiving the response from the coded MCP server, the session still remains active. Is this expected behavior on UiPath’s side?
I have attached the relevant code and the execution log of Coded MCP server. Please take a look and let me know if I am missing anything in the code or if there are any additional steps I need to perform.
MCPCode.txt (1.2 KB)
CodedMCPServerLogs.txt (12.2 KB)