mirror of
https://github.com/modelscope/modelscope.git
synced 2026-05-18 05:05:00 +02:00
Feat: add atomic capabilities - MCPApi (#1426)
This commit is contained in:
@@ -1,177 +0,0 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
|
||||
from modelscope.hub.errors import raise_for_http_status
|
||||
from modelscope.utils.logger import get_logger
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
# MCP API path suffix
|
||||
MCP_API_PATH = '/openapi/v1'
|
||||
|
||||
|
||||
class McpApi:
|
||||
"""MCP (Model Context Protocol) API interface class"""
|
||||
|
||||
def __init__(self, base_api):
|
||||
"""
|
||||
Initialize MCP API
|
||||
|
||||
Args:
|
||||
base_api: HubApi instance for accessing basic API functionality
|
||||
"""
|
||||
self.base_api = base_api
|
||||
# Inherit HubApi's endpoint but add MCP-specific path
|
||||
self.endpoint = base_api.endpoint + MCP_API_PATH
|
||||
self.session = base_api.session
|
||||
self.builder_headers = base_api.builder_headers
|
||||
self.headers = base_api.headers
|
||||
|
||||
def get_mcp_servers(self,
|
||||
token: str,
|
||||
filter: dict = None,
|
||||
page_number: int = 1,
|
||||
page_size: int = 20,
|
||||
search: str = '',
|
||||
endpoint: Optional[str] = None) -> dict:
|
||||
"""
|
||||
Get MCP server list
|
||||
|
||||
Args:
|
||||
token: Authentication token
|
||||
filter: Filter condition dictionary containing the following sub-branches:
|
||||
- category: String type, filter by category
|
||||
- is_hosted: Boolean type, filter by hosting status
|
||||
- tag: JSON string type, filter by tags
|
||||
When category, is_hosted, and tag are all provided, take the intersection of all three
|
||||
page_number: Page number, defaults to 1
|
||||
page_size: Page size, defaults to 20
|
||||
search: Search keyword, defaults to empty string
|
||||
endpoint: API endpoint, defaults to MCP-specific endpoint (inherited from HubApi + /openapi/v1)
|
||||
|
||||
Returns:
|
||||
dict: Dictionary containing MCP server list
|
||||
- mcp_server_list: Detailed MCP server list
|
||||
- total_count: Total count
|
||||
- server_brief_list: Brief server information list
|
||||
"""
|
||||
if not endpoint:
|
||||
endpoint = self.endpoint
|
||||
url = f'{endpoint}/mcp/servers'
|
||||
headers = self.builder_headers(self.headers)
|
||||
headers['Authorization'] = f'Bearer {token}'
|
||||
|
||||
body = {
|
||||
'filter': filter or {},
|
||||
'page_number': page_number,
|
||||
'page_size': page_size,
|
||||
'search': search
|
||||
}
|
||||
|
||||
r = self.session.put(url, headers=headers, json=body)
|
||||
raise_for_http_status(r)
|
||||
|
||||
try:
|
||||
resp = r.json()
|
||||
except requests.exceptions.JSONDecodeError:
|
||||
logger.error(
|
||||
f'Failed to parse JSON response from MCP server list API: {r.text}'
|
||||
)
|
||||
raise
|
||||
|
||||
data = resp.get('data', {})
|
||||
mcp_server_list = data.get('mcp_server_list', [])
|
||||
server_brief_list = [{
|
||||
'name': item.get('name', ''),
|
||||
'description': item.get('description', '')
|
||||
} for item in mcp_server_list]
|
||||
return {
|
||||
'mcp_server_list': mcp_server_list,
|
||||
'total_count': data.get('total_count', 0),
|
||||
'server_brief_list': server_brief_list
|
||||
}
|
||||
|
||||
def get_mcp_server_operational(self,
|
||||
token: str,
|
||||
endpoint: Optional[str] = None) -> dict:
|
||||
"""
|
||||
Get user-hosted MCP server list
|
||||
|
||||
Args:
|
||||
token: Authentication token
|
||||
endpoint: API endpoint, defaults to MCP-specific endpoint (inherited from HubApi + /openapi/v1)
|
||||
|
||||
Returns:
|
||||
dict: Dictionary containing MCP server list
|
||||
- mcp_server_list: Detailed MCP server list
|
||||
- total_count: Total count
|
||||
- server_brief_list: Brief server information list
|
||||
"""
|
||||
if not endpoint:
|
||||
endpoint = self.endpoint
|
||||
url = f'{endpoint}/mcp/servers/operational'
|
||||
headers = self.builder_headers(self.headers)
|
||||
headers['Authorization'] = f'Bearer {token}'
|
||||
|
||||
r = self.session.get(url, headers=headers)
|
||||
raise_for_http_status(r)
|
||||
|
||||
try:
|
||||
resp = r.json()
|
||||
except requests.exceptions.JSONDecodeError:
|
||||
logger.error(
|
||||
f'Failed to parse JSON response from MCP server operational API: {r.text}'
|
||||
)
|
||||
raise
|
||||
|
||||
data = resp.get('data', {})
|
||||
mcp_server_list = data.get('mcp_server_list', [])
|
||||
server_brief_list = [{
|
||||
'name': item.get('name', ''),
|
||||
'description': item.get('description', '')
|
||||
} for item in mcp_server_list]
|
||||
return {
|
||||
'mcp_server_list': mcp_server_list,
|
||||
'total_count': data.get('total_count', 0),
|
||||
'server_brief_list': server_brief_list
|
||||
}
|
||||
|
||||
def get_mcp_server_special(self,
|
||||
server_id: str,
|
||||
token: str,
|
||||
get_operational_url: bool = False,
|
||||
endpoint: Optional[str] = None) -> dict:
|
||||
"""
|
||||
Get specific MCP server details
|
||||
|
||||
Args:
|
||||
server_id: Server ID
|
||||
token: Authentication token
|
||||
get_operational_url: Whether to get operational URL, defaults to False
|
||||
endpoint: API endpoint, defaults to MCP-specific endpoint (inherited from HubApi + /openapi/v1)
|
||||
|
||||
Returns:
|
||||
dict: Dictionary containing MCP server details
|
||||
"""
|
||||
if not endpoint:
|
||||
endpoint = self.endpoint
|
||||
url = f'{endpoint}/mcp/servers/{server_id}'
|
||||
headers = self.builder_headers(self.headers)
|
||||
headers['Authorization'] = f'Bearer {token}'
|
||||
params = {
|
||||
'get_operational_url': str(get_operational_url).lower()
|
||||
} if get_operational_url else {}
|
||||
|
||||
r = self.session.get(url, headers=headers, params=params)
|
||||
raise_for_http_status(r)
|
||||
|
||||
try:
|
||||
resp = r.json()
|
||||
except requests.exceptions.JSONDecodeError:
|
||||
logger.error(
|
||||
f'Failed to parse JSON response from MCP server special API: {r.text}'
|
||||
)
|
||||
raise
|
||||
return resp.get('data', {})
|
||||
339
modelscope/hub/mcp_api.py
Normal file
339
modelscope/hub/mcp_api.py
Normal file
@@ -0,0 +1,339 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
"""
|
||||
MCP (Model Context Protocol) API interface for ModelScope Hub.
|
||||
|
||||
This module provides a simple interface to interact with
|
||||
ModelScope MCP plaza (https://www.modelscope.cn/mcp).
|
||||
"""
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import requests
|
||||
|
||||
from modelscope.hub.api import HubApi, ModelScopeConfig
|
||||
from modelscope.hub.errors import raise_for_http_status
|
||||
from modelscope.utils.logger import get_logger
|
||||
|
||||
# Configure logging
|
||||
logger = get_logger()
|
||||
|
||||
# MCP API path
|
||||
MCP_API_PATH = '/openapi/v1/mcp/servers'
|
||||
|
||||
|
||||
class MCPApiError(Exception):
|
||||
"""Base exception for MCP API errors."""
|
||||
pass
|
||||
|
||||
|
||||
class MCPApiRequestError(MCPApiError):
|
||||
"""Exception raised when MCP API request fails."""
|
||||
pass
|
||||
|
||||
|
||||
class MCPApiResponseError(MCPApiError):
|
||||
"""Exception raised when MCP API response is invalid."""
|
||||
pass
|
||||
|
||||
|
||||
class MCPApi(HubApi):
|
||||
"""
|
||||
MCP (Model Context Protocol) API interface class.
|
||||
|
||||
This class provides interfaces to interact with ModelScope MCP servers,
|
||||
such as to list, deploy and manage MCP servers.
|
||||
|
||||
Note: MCPApi inherits login() from HubApi for authentication.
|
||||
Different methods have different token requirements - see individual method docs.
|
||||
"""
|
||||
|
||||
def __init__(self, endpoint: Optional[str] = None) -> None:
|
||||
"""
|
||||
Initialize MCP API.
|
||||
|
||||
Args:
|
||||
endpoint: The modelscope server address. Defaults to None (uses default endpoint).
|
||||
"""
|
||||
super().__init__(endpoint=endpoint)
|
||||
|
||||
self.mcp_base_url = self.endpoint + MCP_API_PATH
|
||||
|
||||
@staticmethod
|
||||
def _handle_response(r: requests.Response) -> Dict[str, Any]:
|
||||
"""
|
||||
Handle HTTP response with unified error handling and JSON parsing.
|
||||
|
||||
Args:
|
||||
r: requests Response object
|
||||
|
||||
Returns:
|
||||
Parsed response data dict
|
||||
|
||||
Raises:
|
||||
MCPApiResponseError: If JSON parsing fails
|
||||
"""
|
||||
try:
|
||||
resp = r.json()
|
||||
except requests.exceptions.JSONDecodeError as e:
|
||||
logger.error(f'JSON parsing failed: {e}')
|
||||
logger.error(f'Response content: {r.text}')
|
||||
raise MCPApiResponseError(f'Invalid JSON response: {e}') from e
|
||||
|
||||
return resp.get('data', {})
|
||||
|
||||
@staticmethod
|
||||
def _get_server_name_from_id(server_id: str) -> str:
|
||||
"""Extract server name from server ID."""
|
||||
if '/' in server_id:
|
||||
return server_id.split('/', 1)[1]
|
||||
return server_id
|
||||
|
||||
def _get_cookies(self, token: Optional[str] = None):
|
||||
"""Get cookies for authentication."""
|
||||
if token:
|
||||
return self.get_cookies(access_token=token)
|
||||
else:
|
||||
return ModelScopeConfig.get_cookies()
|
||||
|
||||
def list_mcp_servers(self,
|
||||
token: Optional[str] = None,
|
||||
filter: Optional[Dict[str, Any]] = None,
|
||||
total_count: Optional[int] = 20,
|
||||
search: Optional[str] = '') -> Dict[str, Any]:
|
||||
"""
|
||||
List available MCP servers, if (optional) token is presented, this would return private MCP servers as well.
|
||||
|
||||
Args:
|
||||
token: Optional access token for authentication
|
||||
filter: Optional filters to apply to the search
|
||||
- 'category': str, server category, e.g. 'communication'
|
||||
- 'tag': str, server tag, e.g. 'social-media'
|
||||
- 'is_hosted': bool, server is hosted
|
||||
When all three are passed in, the intersection is taken.
|
||||
total_count: Number of servers to return, max 100, default 20
|
||||
search: Optional search query string,e.g. Chinese service name, English service name, author/owner username
|
||||
You can combine `filter` and `search` to retrieve desired MCP servers.
|
||||
|
||||
Returns:
|
||||
Dict containing:
|
||||
- total_count: Total number of servers
|
||||
- servers: List of server dictionaries with name, id, description
|
||||
|
||||
Raises:
|
||||
MCPApiRequestError: If API request fails (network, server errors)
|
||||
MCPApiResponseError: If response format is invalid or JSON parsing fails
|
||||
|
||||
Authentication:
|
||||
Optional, only required if you wish to retrieve private MCP servers.
|
||||
You may leverage the token parameter for one-time authentication, or use api.login()
|
||||
|
||||
Returns:
|
||||
{
|
||||
'total_count': 20,
|
||||
'servers': [
|
||||
{'name': 'ServerA', 'id': '@demo/ServerA', 'description': 'This is a demo server for xxx.'},
|
||||
{'name': 'ServerB', 'id': '@demo/ServerB', 'description': 'This is another demo server.'},
|
||||
...
|
||||
]
|
||||
}
|
||||
"""
|
||||
|
||||
if total_count is None or total_count < 1 or total_count > 100:
|
||||
raise ValueError('total_count must be between 1 and 100')
|
||||
|
||||
body = {
|
||||
'filter': filter or {},
|
||||
'page_number': 1,
|
||||
'page_size': total_count,
|
||||
'search': search
|
||||
}
|
||||
|
||||
try:
|
||||
cookies = self._get_cookies(token)
|
||||
r = self.session.put(
|
||||
url=self.mcp_base_url,
|
||||
headers=self.builder_headers(self.headers),
|
||||
json=body,
|
||||
cookies=cookies)
|
||||
raise_for_http_status(r)
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error('Failed to get MCP servers: %s', e)
|
||||
raise MCPApiRequestError(f'Failed to get MCP servers: {e}') from e
|
||||
|
||||
data = self._handle_response(r)
|
||||
mcp_server_list = data.get('mcp_server_list', [])
|
||||
mcp_config_list = [{
|
||||
'name': item.get('name', ''),
|
||||
'id': item.get('id', ''),
|
||||
'description': item.get('description', '')
|
||||
} for item in mcp_server_list]
|
||||
|
||||
return {
|
||||
'total_count': data.get('total_count', 0),
|
||||
'servers': mcp_config_list
|
||||
}
|
||||
|
||||
def list_operational_mcp_servers(self,
|
||||
token: str = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Get list of operational MCP servers that have been triggered hosting service by the user.
|
||||
|
||||
Returns:
|
||||
Dict containing:
|
||||
- total_counts: Total number of operational servers
|
||||
- servers: List of server info with name, id, description
|
||||
|
||||
Raises:
|
||||
MCPApiRequestError: If authentication fails or API request fails
|
||||
MCPApiResponseError: If response format is invalid or JSON parsing fails
|
||||
|
||||
Returns:
|
||||
{
|
||||
'total_count': 10,
|
||||
'servers': [
|
||||
{
|
||||
'name': 'ServerA',
|
||||
"id": "@Group1/ServerA",
|
||||
'description': 'This is a demo server for xxx.'
|
||||
'mcp_servers': [
|
||||
{
|
||||
'type': 'sse',
|
||||
'url': 'https://mcp.api-inference.modelscope.net/{uuid}/sse'
|
||||
},
|
||||
{
|
||||
'type': 'streamable_http',
|
||||
'url': 'https://mcp.api-inference.modelscope.net/{uuid}/streamable_http'
|
||||
},
|
||||
...
|
||||
]
|
||||
},
|
||||
...
|
||||
]
|
||||
}
|
||||
"""
|
||||
url = f'{self.mcp_base_url}/operational'
|
||||
headers = self.builder_headers(self.headers)
|
||||
|
||||
# Ensure a valid token is provided for operational servers
|
||||
if not token:
|
||||
raise ValueError(
|
||||
'Token is required for accessing operational MCP servers')
|
||||
|
||||
try:
|
||||
cookies = self._get_cookies(token)
|
||||
r = self.session.get(url, headers=headers, cookies=cookies)
|
||||
raise_for_http_status(r)
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f'Failed to get operational MCP servers: {e}')
|
||||
raise MCPApiRequestError(
|
||||
f'Failed to get operational MCP servers: {e}') from e
|
||||
|
||||
logger.debug(f'Response status code: {r.status_code}')
|
||||
|
||||
data = self._handle_response(r)
|
||||
mcp_server_list = data.get('mcp_server_list', [])
|
||||
|
||||
mcp_config_list = []
|
||||
for item in mcp_server_list:
|
||||
mcp_config = {}
|
||||
mcp_config['name'] = item.get('name', '')
|
||||
mcp_config['id'] = item.get('id', '')
|
||||
mcp_config['description'] = item.get('description', '')
|
||||
mcp_config['mcp_servers'] = []
|
||||
for operational_url in item.get('operational_urls', []):
|
||||
mcp_config['mcp_servers'].append({
|
||||
'type':
|
||||
operational_url.get('url').split('/')[-1],
|
||||
'url':
|
||||
operational_url.get('url', '')
|
||||
})
|
||||
mcp_config_list.append(mcp_config)
|
||||
return {
|
||||
'total_count': data.get('total_count', 0),
|
||||
'servers': mcp_config_list
|
||||
}
|
||||
|
||||
def get_mcp_server(self,
|
||||
server_id: str,
|
||||
token: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Get detailed information for a specific MCP Server,
|
||||
a valid token shall be provided if the MCP server is private.
|
||||
|
||||
Args:
|
||||
server_id: MCP server ID (e.g., "@amap/amap-maps")
|
||||
token: Optional access token for authentication
|
||||
|
||||
Returns:
|
||||
Dict containing:
|
||||
- name: Server name
|
||||
- description: Server description
|
||||
- id: Server ID
|
||||
- service_config: Connection configuration with type and url
|
||||
|
||||
Raises:
|
||||
ValueError: If server_id is empty or None
|
||||
MCPApiRequestError: If API request fails or server not found
|
||||
MCPApiResponseError: If response format is invalid or JSON parsing fails
|
||||
|
||||
Returns:
|
||||
{
|
||||
'name': 'ServerA',
|
||||
'description': 'This is a demo server for xxx.',
|
||||
'id': '@demo/serverA',
|
||||
'servers': [
|
||||
{
|
||||
'type': 'sse',
|
||||
'url': 'https://mcp.api-inference.modelscope.net/{uuid}/sse'
|
||||
},
|
||||
{
|
||||
'type': 'streamable_http',
|
||||
'url': 'https://mcp.api-inference.modelscope.net/{uuid}/streamable_http'
|
||||
}
|
||||
...
|
||||
]
|
||||
}
|
||||
"""
|
||||
if not server_id:
|
||||
raise ValueError('server_id cannot be empty')
|
||||
|
||||
url = f'{self.mcp_base_url}/{server_id}'
|
||||
headers = self.builder_headers(self.headers)
|
||||
|
||||
try:
|
||||
cookies = self._get_cookies(token)
|
||||
r = self.session.get(
|
||||
url,
|
||||
headers=headers,
|
||||
params={'get_operational_url':
|
||||
True}, # Always get operational URLs
|
||||
cookies=cookies)
|
||||
raise_for_http_status(r)
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f'Failed to get MCP server {server_id}: {e}')
|
||||
raise MCPApiRequestError(
|
||||
f'Failed to get MCP server {server_id}: {e}') from e
|
||||
|
||||
data = self._handle_response(r)
|
||||
|
||||
result = {
|
||||
'name': data.get('name', ''),
|
||||
'description': data.get('description', ''),
|
||||
'id': data.get('id', '')
|
||||
}
|
||||
|
||||
server_id = data.get('id', '')
|
||||
server_name = MCPApi._get_server_name_from_id(server_id)
|
||||
|
||||
operational_urls = data.get('operational_urls', [])
|
||||
mcp_config_list = []
|
||||
if server_name and operational_urls:
|
||||
for operational_url in operational_urls:
|
||||
mcp_config = {
|
||||
'type': operational_url.get('url').split('/')[-1],
|
||||
'url': operational_url.get('url', '')
|
||||
}
|
||||
mcp_config_list.append(mcp_config)
|
||||
|
||||
result['servers'] = mcp_config_list
|
||||
return result
|
||||
@@ -1,5 +1 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
from .api import McpApi
|
||||
|
||||
__all__ = ['McpApi']
|
||||
68
tests/mcp/test_mcp_api.py
Normal file
68
tests/mcp/test_mcp_api.py
Normal file
@@ -0,0 +1,68 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
import unittest
|
||||
|
||||
from modelscope.hub.mcp_api import MCPApi
|
||||
from modelscope.utils.logger import get_logger
|
||||
from modelscope.utils.test_utils import TEST_ACCESS_TOKEN1, test_level
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
class MCPApiTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures before each test method."""
|
||||
self.api = MCPApi()
|
||||
self.api.login(TEST_ACCESS_TOKEN1)
|
||||
|
||||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
||||
def test_list_mcp_servers(self):
|
||||
"""Test list_mcp_servers functionality and validation."""
|
||||
result = self.api.list_mcp_servers(total_count=5)
|
||||
|
||||
# Verify response structure and content
|
||||
self.assertIn('total_count', result)
|
||||
self.assertIn('servers', result)
|
||||
self.assertGreater(result['total_count'], 0)
|
||||
self.assertGreater(len(result['servers']), 0)
|
||||
|
||||
# Verify server structure
|
||||
server = result['servers'][0]
|
||||
for field in ['name', 'id', 'description']:
|
||||
self.assertIn(field, server)
|
||||
|
||||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
||||
def test_list_operational_mcp_servers(self):
|
||||
"""Test list_operational_mcp_servers functionality."""
|
||||
result = self.api.list_operational_mcp_servers()
|
||||
|
||||
# Verify response structure - corrected field names
|
||||
for field in ['total_count', 'servers']:
|
||||
self.assertIn(field, result)
|
||||
|
||||
# Verify servers structure if exists
|
||||
if result['servers']:
|
||||
first_server = result['servers'][0]
|
||||
for field in ['name', 'id', 'description', 'mcp_servers']:
|
||||
self.assertIn(field, first_server)
|
||||
|
||||
# Verify mcp_servers configuration if exists
|
||||
if first_server['mcp_servers']:
|
||||
first_config = first_server['mcp_servers'][0]
|
||||
self.assertIn('type', first_config)
|
||||
self.assertIn('url', first_config)
|
||||
self.assertTrue(first_config['url'].startswith('https://'))
|
||||
|
||||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
||||
def test_get_mcp_server(self):
|
||||
"""Test get_mcp_server functionality and validation."""
|
||||
result = self.api.get_mcp_server('@modelcontextprotocol/fetch')
|
||||
|
||||
# Verify response structure
|
||||
for field in ['name', 'id', 'description', 'servers']:
|
||||
self.assertIn(field, result)
|
||||
self.assertEqual(result['id'], '@modelcontextprotocol/fetch')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user