Skip to content

Commit 3a34cac

Browse files
committed
use regex
1 parent 1563589 commit 3a34cac

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/mcp-apollo-server/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tokio = { version = "1.44.2", features = [
3131
] }
3232
tracing.workspace = true
3333
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
34+
regex = "1.11.1"
3435

3536
[dev-dependencies]
3637
insta = "1.42.2"

crates/mcp-apollo-server/src/operations.rs

+50-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::collections::HashMap;
2-
31
use apollo_compiler::ast::{FragmentDefinition, Selection};
42
use apollo_compiler::{
53
Name, Node, Schema as GraphqlSchema,
64
ast::{Definition, OperationDefinition, Type},
75
parser::Parser,
86
};
7+
use regex::Regex;
98
use rmcp::{
109
model::Tool,
1110
schemars::schema::{
@@ -16,6 +15,7 @@ use rmcp::{
1615
};
1716
use rover_copy::pq_manifest::ApolloPersistedQueryManifest;
1817
use serde::Serialize;
18+
use std::collections::HashMap;
1919

2020
use crate::errors::{McpError, OperationError};
2121
use crate::graphql;
@@ -152,14 +152,8 @@ impl Operation {
152152
fragment_defs: &[&Node<FragmentDefinition>],
153153
) -> String {
154154
let comment_description = comments.and_then(|comments| {
155-
// TODO: there is probably a better way to do this with a regex, but I couldn't figure it out. So here is a crude version
156-
let formatted = comments
157-
.split("\n")
158-
.map(|line| line.trim())
159-
.collect::<Vec<_>>()
160-
.join("\n")
161-
.replace("\n#", "\n");
162-
let trimmed = formatted.trim();
155+
let content = Regex::new(r"(\n|^)\s*#").ok()?.replace_all(comments, "$1");
156+
let trimmed = content.trim();
163157

164158
if trimmed.is_empty() {
165159
None
@@ -1376,6 +1370,52 @@ mod tests {
13761370
);
13771371
}
13781372

1373+
#[test]
1374+
fn tool_comment_description() {
1375+
let operation = Operation::from_document(
1376+
r###"
1377+
# Overridden tool #description
1378+
query GetABZ($state: String!) {
1379+
b {
1380+
d {
1381+
f
1382+
}
1383+
}
1384+
}
1385+
"###,
1386+
&SCHEMA,
1387+
None,
1388+
)
1389+
.unwrap();
1390+
1391+
insta::assert_snapshot!(
1392+
operation.tool.description.as_ref(),
1393+
@r###"Overridden tool #description"###
1394+
);
1395+
}
1396+
1397+
#[test]
1398+
fn tool_empty_comment_description() {
1399+
let operation = Operation::from_document(
1400+
r###"
1401+
#
1402+
1403+
#
1404+
query GetABZ($state: String!) {
1405+
id
1406+
}
1407+
"###,
1408+
&SCHEMA,
1409+
None,
1410+
)
1411+
.unwrap();
1412+
1413+
insta::assert_snapshot!(
1414+
operation.tool.description.as_ref(),
1415+
@r###"The returned value is optional and has type `String`"###
1416+
);
1417+
}
1418+
13791419
#[test]
13801420
fn it_extracts_operations_from_apollo_pq_manifest() {
13811421
// The inner types needed to construct one of these are not exported,

0 commit comments

Comments
 (0)