|
6 | 6 |
|
7 | 7 | package modelengine.fit.jober.aipp.util; |
8 | 8 |
|
9 | | -import modelengine.fitframework.inspection.Validation; |
| 9 | +import modelengine.fitframework.util.StringUtils; |
| 10 | + |
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
10 | 13 |
|
11 | 14 | /** |
12 | 15 | * 大模型上下文协议相关工具方法。 |
|
15 | 18 | * @since 2025-07-11 |
16 | 19 | */ |
17 | 20 | public class McpUtils { |
18 | | - private static final String SSE_ENDPOINT_SPLIT_DELIMITER = "/"; |
19 | | - |
20 | 21 | /** |
21 | 22 | * 获取 {@code baseUrl} 部分。 |
22 | 23 | * |
23 | 24 | * @param url 目标地址。 |
24 | 25 | * @return {@code baseUrl} 部分。 |
25 | | - * @throws IllegalArgumentException 当目标地址不包含 {@code sseEndpoint} 时。 |
| 26 | + * @throws IllegalArgumentException 当目标地址不合法时。 |
26 | 27 | */ |
27 | 28 | public static String getBaseUrl(String url) { |
28 | | - String[] splits = url.split(SSE_ENDPOINT_SPLIT_DELIMITER); |
29 | | - Validation.greaterThan(splits.length, 3, "The url is wrong. [url={0}]", url); |
30 | | - return url.substring(0, url.length() - splits[splits.length - 1].length() - 1); |
| 29 | + try { |
| 30 | + URI uri = new URI(url); |
| 31 | + String baseUrl = uri.getScheme() + "://" + uri.getHost(); |
| 32 | + if (uri.getPort() != -1) { |
| 33 | + baseUrl += ":" + uri.getPort(); |
| 34 | + } |
| 35 | + return baseUrl; |
| 36 | + } catch (URISyntaxException e) { |
| 37 | + throw new IllegalArgumentException(StringUtils.format("The url is wrong. [url={0}]", url)); |
| 38 | + } |
31 | 39 | } |
32 | 40 |
|
33 | 41 | /** |
34 | 42 | * 获取 {@code sseEndpoint} 部分。 |
35 | 43 | * |
36 | 44 | * @param url 目标地址。 |
37 | 45 | * @return {@code sseEndpoint} 部分。 |
38 | | - * @throws IllegalArgumentException 当目标地址不包含 {@code sseEndpoint} 时。 |
| 46 | + * @throws IllegalArgumentException 当目标地址不合法时。 |
39 | 47 | */ |
40 | 48 | public static String getSseEndpoint(String url) { |
41 | | - String[] splits = url.split(SSE_ENDPOINT_SPLIT_DELIMITER); |
42 | | - Validation.greaterThan(splits.length, 3, "The url is wrong. [url={0}]", url); |
43 | | - return SSE_ENDPOINT_SPLIT_DELIMITER + splits[splits.length - 1]; |
| 49 | + try { |
| 50 | + URI uri = new URI(url); |
| 51 | + return uri.getPath(); |
| 52 | + } catch (URISyntaxException e) { |
| 53 | + throw new IllegalArgumentException(StringUtils.format("The url is wrong. [url={0}]", url)); |
| 54 | + } |
44 | 55 | } |
45 | 56 | } |
0 commit comments