77 "os"
88 "regexp"
99 "strings"
10+ "unicode"
1011
1112 "mcplib"
1213)
@@ -119,7 +120,7 @@ func (s *MDownService) GetTools() []mcplib.Tool {
119120 },
120121 {
121122 Name : "search" ,
122- Description : "Locates and returns sections that correspond to a search query or list of search queries." ,
123+ Description : "Locates and returns sections that correspond to a search query or list of search queries. If there are no results try with less words " ,
123124 InputSchema : map [string ]any {
124125 "type" : "object" ,
125126 "properties" : map [string ]any {
@@ -156,16 +157,54 @@ func (s *MDownService) handleListSections(args map[string]any) (any, error) {
156157 return map [string ]any {"sections" : out }, nil
157158}
158159
160+ // normalizeSectionName trims surrounding whitespace and punctuation and lowercases the name
161+ func normalizeSectionName (s string ) string {
162+ s = strings .TrimSpace (s )
163+ s = strings .TrimFunc (s , func (r rune ) bool {
164+ return unicode .IsSpace (r ) || unicode .IsPunct (r )
165+ })
166+ return strings .ToLower (s )
167+ }
168+
169+ // handleGetContents retrieves the full content of a given section,
170+ // matching names case-insensitively and ignoring surrounding punctuation.
159171func (s * MDownService ) handleGetContents (args map [string ]any ) (any , error ) {
160172 name , ok := args ["section" ].(string )
161173 if ! ok || name == "" {
162174 return nil , fmt .Errorf ("section is required" )
163175 }
164176 content , ok := s .sectionContent [name ]
165177 if ! ok {
178+ norm := normalizeSectionName (name )
179+ // try normalized exact match
180+ for secName , secContent := range s .sectionContent {
181+ if normalizeSectionName (secName ) == norm {
182+ content = secContent
183+ ok = true
184+ break
185+ }
186+ }
187+ }
188+ if ! ok {
189+ norm := normalizeSectionName (name )
190+ // try normalized substring match
191+ for secName , secContent := range s .sectionContent {
192+ if strings .Contains (normalizeSectionName (secName ), norm ) {
193+ content = secContent
194+ ok = true
195+ break
196+ }
197+ }
198+ }
199+ if ! ok {
200+ return "Section not found" , nil
166201 return nil , fmt .Errorf ("section %q not found" , name )
167202 }
168- return map [string ]any {"content" : content }, nil
203+ res := map [string ]any {"content" : content }
204+ if res != nil {
205+ return res , nil
206+ }
207+ return "" , fmt .Errorf ("Cannot find contents" )
169208}
170209
171210func (s * MDownService ) handleShowContents (args map [string ]any ) (any , error ) {
@@ -174,8 +213,10 @@ func (s *MDownService) handleShowContents(args map[string]any) (any, error) {
174213 return nil , err
175214 }
176215 content := res .(map [string ]any )["content" ].(string )
177- fmt .Println (content )
178- return nil , nil
216+ if content != "" {
217+ return content , nil
218+ }
219+ return nil , fmt .Errorf ("Cannot find contents" )
179220}
180221
181222func (s * MDownService ) handleShowTree (args map [string ]any ) (any , error ) {
@@ -204,8 +245,12 @@ func (s *MDownService) handleSearch(args map[string]any) (any, error) {
204245 }
205246 var matches []string
206247 for name , content := range s .sectionContent {
248+ // Perform case-insensitive matching for search terms, supporting multi-word phrases.
249+ lowerName := strings .ToLower (name )
250+ lowerContent := strings .ToLower (content )
207251 for _ , term := range terms {
208- if strings .Contains (name , term ) || strings .Contains (content , term ) {
252+ lowerTerm := strings .ToLower (term )
253+ if strings .Contains (lowerName , lowerTerm ) || strings .Contains (lowerContent , lowerTerm ) {
209254 matches = append (matches , name )
210255 break
211256 }
0 commit comments