@@ -57,9 +57,96 @@ func (g *Generator) GenerateToolFile(filePath, toolName string, config map[strin
5757 return fmt .Errorf ("failed to execute template: %w" , err )
5858 }
5959
60+ // After generating the tool file, regenerate the __init__.py file
61+ toolsDir := filepath .Dir (filePath )
62+ if err := g .RegenerateToolsInit (toolsDir ); err != nil {
63+ return fmt .Errorf ("failed to regenerate __init__.py: %w" , err )
64+ }
65+
66+ return nil
67+ }
68+
69+ // RegenerateToolsInit regenerates the __init__.py file in the tools directory
70+ func (g * Generator ) RegenerateToolsInit (toolsDir string ) error {
71+ // Scan the tools directory for Python files
72+ tools , err := g .ScanToolsDirectory (toolsDir )
73+ if err != nil {
74+ return fmt .Errorf ("failed to scan tools directory: %w" , err )
75+ }
76+
77+ // Generate the __init__.py content
78+ content := g .generateInitContent (tools )
79+
80+ // Write the __init__.py file
81+ initPath := filepath .Join (toolsDir , "__init__.py" )
82+ if err := os .WriteFile (initPath , []byte (content ), 0644 ); err != nil {
83+ return fmt .Errorf ("failed to write __init__.py: %w" , err )
84+ }
85+
6086 return nil
6187}
6288
89+ // ScanToolsDirectory scans the tools directory and returns a list of tool names
90+ func (g * Generator ) ScanToolsDirectory (toolsDir string ) ([]string , error ) {
91+ var tools []string
92+
93+ // Read the directory
94+ entries , err := os .ReadDir (toolsDir )
95+ if err != nil {
96+ return nil , fmt .Errorf ("failed to read tools directory: %w" , err )
97+ }
98+
99+ // Find all Python files (excluding __init__.py)
100+ for _ , entry := range entries {
101+ if entry .IsDir () {
102+ continue
103+ }
104+
105+ name := entry .Name ()
106+ if strings .HasSuffix (name , ".py" ) && name != "__init__.py" {
107+ // Extract tool name (filename without .py extension)
108+ toolName := strings .TrimSuffix (name , ".py" )
109+ tools = append (tools , toolName )
110+ }
111+ }
112+
113+ return tools , nil
114+ }
115+
116+ // generateInitContent generates the content for the __init__.py file
117+ func (g * Generator ) generateInitContent (tools []string ) string {
118+ var content strings.Builder
119+
120+ // Add the header comment
121+ content .WriteString (`"""Tools package for knowledge-assistant MCP server.
122+
123+ This file is automatically generated by the dynamic loading system.
124+ Do not edit manually - it will be overwritten when tools are loaded.
125+ """
126+
127+ ` )
128+
129+ // Add import statements
130+ for _ , tool := range tools {
131+ content .WriteString (fmt .Sprintf ("from .%s import %s\n " , tool , tool ))
132+ }
133+
134+ // Add empty line
135+ content .WriteString ("\n " )
136+
137+ // Add __all__ list
138+ content .WriteString ("__all__ = [" )
139+ for i , tool := range tools {
140+ if i > 0 {
141+ content .WriteString (", " )
142+ }
143+ content .WriteString (fmt .Sprintf (`"%s"` , tool ))
144+ }
145+ content .WriteString ("]\n " )
146+
147+ return content .String ()
148+ }
149+
63150// getUnifiedTemplate returns the unified tool template with commented examples
64151func (g * Generator ) getUnifiedTemplate () string {
65152 return `"""{{.ToolNameTitle}} tool for MCP server.{{if .description}}
0 commit comments