Why are we receiving 401 errors when our authorized applications are called despite having the highest version?

This topic was automatically generated from Slack. You can find the original thread here.

We have purchased the highest version, but we found that many applications have been successfully authorized, but when our Agent calls the authorized tools, we often encounter 401 errors in many applications. We have clearly authorized the applications, please help us thank you:An error occurred while calling the tool: Error: Status code: 401
Body: {
“error”: “Unauthorized”
}

That sounds like maybe the authentication to Pipedream’s API is failing. Are you using one of the SDKs? Or the REST API? It sounds like maybe you are using the REST API and not refreshing the Pipedream access token? Or perhaps there’s an issue with how you’re initializing the SDK client?

HI We use the SDK to get access_token and then use MCP, ultimately we use the ADK’s McpToolset module to use tools, here is our code:import os
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
from pipedream import Pipedream
import json
from datetime import datetime
import tqdm
PIPEDREAM_PROJECT_ID = os.getenv(“TOOL_PIPEDREAM_PROJECT_ID”)
PIPEDREAM_ENVIRONMENT = os.getenv(“TOOL_PIPEDREAM_ENVIRONMENT”)
PIPEDREAM_CLIENT_ID = os.getenv(“TOOL_PIPEDREAM_CLIENT_ID”)
PIPEDREAM_CLIENT_SECRET = os.getenv(“TOOL_PIPEDREAM_CLIENT_SECRET”)
pd = Pipedream(
project_id=PIPEDREAM_PROJECT_ID,
project_environment=PIPEDREAM_ENVIRONMENT,
client_id=PIPEDREAM_CLIENT_ID,
client_secret=PIPEDREAM_CLIENT_SECRET,
)
access_token = pd.raw_access_token
DEV_EXTERNAL_USER_ID = “8f47d8b9581a4e09b566ec7894509a9a”
server_url = “https://remote.mcp.pipedream.net
# Configure MCP client with authentication headers
headers = {
“Authorization”: f"Bearer {access_token}",
“x-pd-project-id”: PIPEDREAM_PROJECT_ID, # proj_xxxxxxx
“x-pd-environment”: PIPEDREAM_ENVIRONMENT, # development | production
“x-pd-external-user-id”: DEV_EXTERNAL_USER_ID, # the user’s ID from your system
“x-pd-app-slug”: “linkedin”, # notion, linear, gmail, etc
“x-pd-account-id”: ‘xxx’ # this is the real account id
}
# Create MCP client connection
async with streamablehttp_client(server_url, headers=headers) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
# Now you can use the session to call tools
#list_tool2 = await session.list_tools()
result = await session.call_tool(
“linkedin-get-current-member-profile”,
{
“instruction”: “Get the current authenticated member’s profile information to verify LinkedIn integration is working properly.”
}
)
print(result)

Additionally, I have another question: If a process takes a long time, do we need to manually refresh the access_token during that time? Currently, I see online that it doesn’t need to be refreshed automatically

The SDK will automatically refresh the access token for you

Regarding our own MCP, when we create the MCP, we will obtain the token from your SDK. During the subsequent usage period of our MCP (which may last for several hours), will this token expire? If it does expire, does your token support the native token refresh mechanism of the MCP? If it does not support this mechanism, do we need to manually reconnect to obtain a new token?